开发者社区> 问答> 正文

JSON解析报错 怎么解决?贴出错误和代码 ?报错

{"res":"01","info":{"SongName":"L\u1ee1 y\u00eaubbbbbbbb","WordNum":"2","PyCode":"LY","Stroke":"0","SingerName1":"Hu\u1ef3nh Nh\u1eadt Huy","SingerName2":"","FileName":"2","Lang":"1","SongTypeID":"3","SingerID1":"123","SingerID2":"456","MType":"1","yTrack":"0","bTrack":"1","yVolume":"92","bVolume":"92","NewSong":"1","style":"9","SongNameSpell":"aaa","SingerNameSpell1":null,"SingerNameSpell2":null}}

这是我的JSON串
然后我的解析是这样做的

JSONObject obj = new JSONObject(response.toString());
JSONArray arr = obj.getJSONArray("info");
for (int i = 0; i < arr.length(); i++) {
JSONObject subObj = arr.getJSONObject(i);
String sn = subObj.getString("SongName");
String w = subObj.getString("WordNum");
String p = subObj.getString("PyCode");
String st = subObj.getString("Stroke");
String singer1 = subObj.getString("SingerName1");
。。。。。。。
然后打印出解析的JSON串
}

报错是
Value {"Lang":"1","yVolume":"92","MType":"1","SongNameSpell":"aaa","Stroke":"0","SongName":"Lỡ yêubbbbbbbb","SingerNameSpell1":null,"SingerNameSpell2":null,"yTrack":"0","SongTypeID":"3","PyCode":"LY","style":"9","bVolume":"92","FileName":"2","SingerName1":"Huỳnh Nhật Huy","WordNum":"2","SingerName2":"","SingerID2":"456","NewSong":"1","SingerID1":"123","bTrack":"1"}
at info of type org.json.JSONObject cannot be converted to JSONArray

请问这是什么情况 求解答啊。。

展开
收起
爱吃鱼的程序员 2020-06-23 13:44:46 822 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    JSONObject不能被转化为JSONArray,

    json字符串不是JSONArray的类型啊当然转换不了成JSONArray

    换个json工具fastJson解析就一行代码JSONObject.parseObject("json字符串")

    你这个里面都没有数组,所以不需要用JSONArray,直接像第一个解析那样用JSONObject就行。然后不需要for循环,直接就拿你需要数据的字段就可以了

    {“res”:01,"info":{???????}}。都是对象JSONObjectarr=obj.getJSONObject("info");‘

    {“res”:01,"info":[???????]}。如果是这样的话你就没错。注意,区别不大,括号不同。这是数组
    但是如果你要拿到全部数据会很麻烦。可以用第三方的解析工具。Gson、fastJson什么的会简单很多。

    db.execSQL("CREATETABLEminfo(idINTEGERPRIMARYKEYAUTOINCREMENT,nameVARCHAR,ageSMALLINT,SingerNameVARCHAR,WordNumSMALLINT,verSMALLINT,PyCodeSMALLINT,StrokeSMALLINT,SingerName1VARCHAR,SingerName2VARCHAR,FileNameSMALLINT,LangVARCHAR,SongTypeIDSMALLINT,SingerID1SMALLINT,SingerID2SMALLINT,MTypeSMALLINT,yTrackSMALLINT,bTrackSMALLINT,yVolumeSMALLINT,bVolumeSMALLINT,NewSongSMALLINT,styleSMALLINT,SongNameSpellVARCHAR,SongNameSpell1VARCHAR,SongNameSpell2VARCHAR)");

    cv1.put("name","aaaa");
    cv1.put("WordNum","6");
    cv1.put("ver",6);
    cv1.put("age","24");
    cv1.put("PyCode","KD");
    cv1.put("Stroke",4);
    cv1.put("SingerName1","lb");
    cv1.put("SingerName2","ja");
    cv1.put("Lang","0");
    cv1.put("FileName","3");
    cv1.put("SongTypeID",1);
    ‘’‘’
    。。。
    这些我都写了可是解析的时候
    JSONObjectobj=newJSONObject(response.toString());
    JSONArrayarr=obj.getJSONArray("info");
    Stringl=obj.getString("Lang");
    Stringw=obj.getString("SingerName1");
    Log.i("解析后的数据","SingerName1"+w);
    为什么比如说我要打印SingerName1或Lang等等这些值后台会提示System.err:org.json.JSONException:NovalueforSingerName1呢??

    很明显:info这个字段对应的是JsonObject而不是JsonArray,所以你转换就会抛异常啊

    JSONObjectobj=newJSONObject(response.toString());
    JSONObjectobject=obj.getJSONObject("info");
    Stringl=object.getString("Lang");
    Stringw=object.getString("SingerName1");
    Log.i("解析后的数据","SingerName1"+w);
    这样的话就没问题了,要区分[]和{}的区别哟~

    2020-06-23 13:45:03
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像