Java 由以下原因引起:com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJSON异常:应为';:';排队
我在stackoverflow上也看到过同样的问题,它告诉我们Json中额外的逗号和空格。但这对我没有帮助 我在POJO中使用了这种格式的jsonJava 由以下原因引起:com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJSON异常:应为';:';排队,java,json,Java,Json,我在stackoverflow上也看到过同样的问题,它告诉我们Json中额外的逗号和空格。但这对我没有帮助 我在POJO中使用了这种格式的json {"binary":[ { "attributeIndex": 4, "attributeValue": "no", "binaryValue": "1,0" }, { "attributeIndex": 4, "attributeValue": "yes", "binaryValue
{"binary":[
{
"attributeIndex": 4,
"attributeValue": "no",
"binaryValue": "1,0"
},
{
"attributeIndex": 4,
"attributeValue": "yes",
"binaryValue": "0,1"
}
]}
波乔
我正在尝试获取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)
在
我的代码:
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();
}
我的结果是
meta:{attributeIndex=0.0,attributeValue=中年,
binaryValue=1,0,0}
及
进行如下更改,然后重试
public class BinaryPojo {
public List<BinPojo> binary;
}
公共类二进制POJO{
公开列表二进制;
}
读取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);
}
URL-feedURL=newurl(“您的json-feed-URL-here”);
Gson Gson=新的Gson();
Reader Reader=新的InputStreamReader(feedURL.openStream());
BinaryPojo BinaryPojo=gson.fromJson(reader,BinaryPojo.class);
List binPojo=binaryPojo.binary;
for(BinPojo结果:BinPojo){
System.out.println(“attributeIndex:+result.attributeIndex”);
System.out.println(“attributeValue:+result.attributeValue”);
System.out.println(“binaryValue:+result.binaryValue”);
}
从获取Gson库啊!我想这是你的问题。您使用的是“JSON Simple”库(
org.JSON.Simple.
),而不是“JSON for Java”库(org.JSON.
)。它们都有名为JSONArray
的类。但是json simple中的org.json.simple.JSONArray
不会像“json for Java”中的org.json.JSONArray
那样从其toString()
方法返回json
您需要在JSONArray
和JSONObject
对象上使用toJSONString()
密切关注您实际使用的库。在你的问题中包括它们(最好有版本号)将使回答这样的问题更容易。类名称在项目中不是唯一的。好吧,您想查看
meta.get(j).toString()中的内容。
。因此,在解析它之前,可以将它存储在一个单独的行中,在gson.fromJson()
调用周围放置一个try
/catch
,如果它抛出异常,则打印出该字符串作为错误消息的一部分。或者在调试器中停止它:只要你已经将该字符串存储在一个变量中,它就会在那里供你检查。你可以看到它期望:
,并且你正在使用你的meta.get(j).toString()传递=
,看起来meta.get(j).toString()提供了错误的结果,并且不给你Json字符串。你的meta类是什么?@NamanGala:我不明白你为什么不干脆做JsonArray meta=gson.fromJson(新文件阅读器(new File)(“File”)),JsonArray.class)代码>?它对我来说也很好(我实例化了我提到的JsonArray
)。并且不需要调用toString
调用,binpojogetdata=gson.fromJson(meta.get(j),BinPojo.class)代码>应该按预期工作。他应该只使用一个Json库。Gson有一个JsonArray
类。这个BinaryPojo
没有用。是的,但这是一个设计问题。这就是OP的特殊代码给他们这个错误的原因,并且回答了这个问题。它让这么多人忙了十几条评论,这至少让它很有趣。
System.out.println("meta: "+meta.get(j).toString());
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();
}
gson null
java.lang.Exception: java.lang.NullPointerException
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:354)
Caused by: java.lang.NullPointerException
public class BinaryPojo {
public List<BinPojo> binary;
}
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);
}