org.json.JSONException:字符19处的未终止对象

时间:2017-07-28 19:14:11

标签: java android json

我一直在android中的示例firebase项目中工作,我手动将值添加到firebase数据库。我将以下数据添加到数据库中,并在解析json时获得以下异常

org.json.JSONException <\ strong>

字符19处的未终止对象

{GK=[{option1=1.5 hour, Question1=HI, answer=OptionC, option3=1hour, option4=None, option2=2hour}, {option1=15, Question1=jkl, answer=OptionD, option3=25, option4=70, option2=45}, {option1=Oil lamp, Question1=jkl, answer=OptionD, option3=Newspaper, option4=None, option2=kindling wood}], Sports=[{option1=FNLAKDS, Question1=gk1, option3=ram, option4=ram, option2=ram}, {option1=FNLAKDS, Question1=gk2, option3=ram, option4=ram, option2=ram}, {option1=FNLAKDS, Question1=gk3, option3=ram, option4=ram, option2=ram}]} 1

因为我在我的字符串中添加了逗号,所以我很困惑这个错误被抛出。任何帮助将不胜感激。

我的安卓代码

  String Jsondatas=dataSnapshot.getValue().toString();
      try {
                        JSONObject jsonobj=new JSONObject(Jsondatas);
                            JSONArray gkArray = jsonobj.getJSONArray("GK");
                            for(int i=0;i<gkArray.length();i++){
                                JSONObject obj_gk = gkArray.getJSONObject(i);

我在这里贴上我的火力棒截图

FireBase Image ScreenShot

1 个答案:

答案 0 :(得分:0)

您尝试解析的json无效。字符串文字应该用双引号括起来,而赋值(1)用=完成。

我已将这些修复程序应用于您的json:

:

我建议您使用json验证器(例如https://jsonformatter.curiousconcept.com/)来帮助您编写有效的json。

紧凑版:

{
   "G":[
      {
         "option1":"1.5 hour",
         "Question1":"HI",
         "answer":"OptionC",
         "option3":"1hour",
         "option4":"None",
         "option2":"2hour"
      },
      {
         "option1":15,
         "Question1":"jkl",
         "answer":"OptionD",
         "option3":25,
         "option4":70,
         "option2":45
      },
      {
         "option1":"Oil lamp",
         "Question1":"jkl",
         "answer":"OptionD",
         "option3":"Newspaper",
         "option4":"None",
         "option2":"kindling wood"
      }
   ],
   "Sports":[
      {
         "option1":"FNLAKDS",
         "Question1":"gk1",
         "option3":"ram",
         "option4":"ram",
         "option2":"ram"
      },
      {
         "option1":"FNLAKDS",
         "Question1":"gk2",
         "option3":"ram",
         "option4":"ram",
         "option2":"ram"
      },
      {
         "option1":"FNLAKDS",
         "Question1":"gk3",
         "option3":"ram",
         "option4":"ram",
         "option2":"ram"
      }
   ]
}