Java 引起:com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:预期的“:”在线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29815642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Caused by: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected ':' at line
提问by Unmesha SreeVeni
I have seen the same questions over stackoverflow, which tells about extra comma and spaces in Json. But it did nt helped me.
我在 stackoverflow 上看到了同样的问题,它讲述了 Json 中额外的逗号和空格。但它没有帮助我。
I am having a json of this format with POJO
我有一个带有 POJO 的这种格式的 json
{"binary":[
{
"attributeIndex": 4,
"attributeValue": "no",
"binaryValue": "1,0"
},
{
"attributeIndex": 4,
"attributeValue": "yes",
"binaryValue": "0,1"
}
]}
POJO
POJO
public class BinPojo {
/**
* @param args
*/
int attributeIndex;
String attributeValue;
String binaryValue;
and
public class BinaryPojo {
/**
* @param args
*/
JSONArray binary;
I am trying to get attributeIndex,attributeValue,binaryValue values But I am getting this error
我正在尝试获取 attributeIndex、attributeValue、binaryValue 值但我收到此错误
Caused by: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected ':' at line 1 column 65
at com.google.gson.Gson.fromJson(Gson.java:818)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
at
在
BinPojo getData = gson.fromJson(meta.get(j).toString(), BinPojo.class);
My code:
我的代码:
JSONArray meta = new JSONArray();
//file read
String setupData = bf.readLine();
BinaryPojo d = gson.fromJson(setupData, BinaryPojo.class);
meta = d.getBinary();
//JSONArray meta which holds the JSON data
for (int j = 0; j < meta.size(); j++) {
BinPojo getData = gson.fromJson(meta.get(j).toString(), BinPojo.class);
System.out.println("gson"+gson.toJson(getData));
int attrIndex = getData.getAttributeIndex();
System.out.println("attrIndex: "+attrIndex);
String attrVal = getData.getAttributeValue();
}
What could be the reason. It is telling that my JSON is bad.how can I debug this.
可能是什么原因。它告诉我我的 JSON 是坏的。我该如何调试它。
EDIT
编辑
Once I print this:
一旦我打印这个:
System.out.println("meta: "+meta.get(j).toString());
My result is
我的结果是
meta: {attributeIndex=0.0, attributeValue=Middleaged, binaryValue=1,0,0}
元:{attributeIndex=0.0,attributeValue=Middleaged,binaryValue=1,0,0}
and
和
for (int j = 0; j < meta.size(); j++) {
System.out.println("meta: "+meta.get(j).toString());
BinPojo getData = null ;
try{
getData = gson.fromJson(meta.get(j).toString(), BinPojo.class);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("gson "+gson.toJson(getData));
int attrIndex = getData.getAttributeIndex();
System.out.println("attrIndex: "+attrIndex);
String attrVal = getData.getAttributeValue();
}
getData is returning null.
getData 返回 null。
gson null
java.lang.Exception: java.lang.NullPointerException
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:354)
Caused by: java.lang.NullPointerException
采纳答案by Andrew Janke
Ah! I think that's your problem. You're using the "JSON-Simple" library (org.json.simple.*
) instead of the "JSON for Java" library (org.json.*
). They both have classes named JSONArray
. But the org.json.simple.JSONArray
from JSON-Simple does not return JSON from its toString()
method like the org.json.JSONArray
from "JSON for Java" does.
啊! 我想那是你的问题。您正在使用“JSON-Simple”库 ( org.json.simple.*
) 而不是“JSON for Java”库 ( org.json.*
)。它们都有名为JSONArray
. 但是org.json.simple.JSONArray
from JSON-Simple 不会toString()
像org.json.JSONArray
from "JSON for Java" 那样从其方法返回 JSON 。
You need to use toJSONString()
instead on your JSONArray
and JSONObject
objects.
您需要toJSONString()
在您的JSONArray
和JSONObject
对象上使用。
Pay close attention to which libraries you're actually using. Including them (preferably with version numbers) in your questions will make answering questions like this easier. Class names are not unique across projects.
密切注意您实际使用的库。在您的问题中包含它们(最好带有版本号)将使回答此类问题更容易。类名在项目中不是唯一的。
回答by Bishan
Make changes as below and try.
进行如下更改并尝试。
public class BinaryPojo {
public List<BinPojo> binary;
}
Read JSON data as below.
读取 JSON 数据如下。
URL feedURL = new URL("your_json_feed_url_here");
Gson gson = new Gson();
Reader reader = new InputStreamReader(feedURL.openStream());
BinaryPojo binaryPojo = gson.fromJson(reader, BinaryPojo.class);
List<BinPojo> binPojo = binaryPojo.binary;
for (BinPojo result : binPojo) {
System.out.println("attributeIndex: " + result.attributeIndex);
System.out.println("attributeValue: " + result.attributeValue);
System.out.println("binaryValue: " + result.binaryValue);
}
Get the Gson library from Gson official site
从Gson 官网获取 Gson 库