{"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
请问这是什么情况 求解答啊。。
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);
这样的话就没问题了,要区分[]和{}的区别哟~
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。