引起:com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:Expected':'在

时间:2015-04-23 06:43:13

标签: java json

我在stackoverflow上看到了同样的问题,它讲述了Json中额外的逗号和空格。但它确实帮助了我。

我正在使用POJO

这种格式的json
 {"binary":[
    {
    "attributeIndex": 4,
    "attributeValue": "no",
    "binaryValue": "1,0"
  },
  {
    "attributeIndex": 4,
    "attributeValue": "yes",
    "binaryValue": "0,1"
  }
]}

POJO

public class BinPojo {

    /**
     * @param args
     */
    int attributeIndex;
    String attributeValue;
    String binaryValue;
and

public class BinaryPojo {

    /**
     * @param args
     */
    JSONArray binary;

我正在尝试获取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);

我的代码:

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();               
}

可能是什么原因。它告诉我的JSON是坏的。我可以调试它。

修改

打印出来之后:

System.out.println("meta: "+meta.get(j).toString());

我的结果是

  

meta:{attributeIndex = 0.0,attributeValue = Middleaged,   binaryValue = 1,0,0}

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返回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

2 个答案:

答案 0 :(得分:1)

进行如下更改并尝试。

public class BinaryPojo {    
    public List<BinPojo> binary;
}

读取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);
}

Gson official site

获取Gson图书馆

答案 1 :(得分:0)

啊!我认为这是你的问题。您正在使用&#34; JSON-Simple&#34;库(org.json.simple.*)而不是&#34; JSON for Java&#34;库(org.json.*)。他们都有名为JSONArray的班级。但是来自JSON-Simple的org.json.simple.JSONArray不会从toString()方法返回JSON,例如来自&#34; JSON for Java&#34;的org.json.JSONArray。确实。

您需要在toJSONString()JSONArray个对象上使用JSONObject

密切关注您实际使用的图书馆。在您的问题中包括它们(最好带有版本号)可以更容易地回答这样的问题。类名在项目中不是唯一的。