{
"count": 5,
"start": 0,
"total": 6736,
"books": [
{
"publisher": "中國電力出版社",
"image": "http://img3.douban.com/mpic/s1957104.jpg",
"title": "Java Enterprise最佳實踐",
"author": [
"The OReilly Java Authors"
]
},
{
"publisher": "北京大學出版社",
"image": "http://img5.douban.com/mpic/s1022519.jpg",
"title": "Java應用程序設計接口(下冊)--窗口工具箱和applet",
"author": [
"(美)James Gosling",
"Frank Yellin",
"Java 小組"
]
}
]
}
我是新手,很多不懂。求前輩們指導一下。就用普通的JsonObject和JsonArray就行,不用gson什麼的。最好是直接將上面的json用代碼解析一下,謝謝了。
try{
String str = "{
"count": 5,
"start": 0,
"total": 6736,
"books": [
{
"publisher": "中國電力出版社",
"image": "http://img3.douban.com/mpic/s1957104.jpg",
"title": "Java Enterprise最佳實踐",
"author": [
"The OReilly Java Authors"
]
},
{
"publisher": "北京大學出版社",
"image": "http://img5.douban.com/mpic/s1022519.jpg",
"title": "Java應用程序設計接口(下冊)--窗口工具箱和applet",
"author": [
"(美)James Gosling",
"Frank Yellin",
"Java 小組"
]
}
]
}"
這個str是字符串 除了前後引號 內部的引號要用\"代替
JSONObject allObj = new JSONObject(str);
JSONArray bookobjs = allObj.getJSONArray("books");
int len = bookobjs.size();
for(int i =0;i<len;i++){
JSONObject obj = bookobjs.get(i);
這裡的obj已經是單獨的一本書了 ;
String title = obj.getString("title");
Log.e("標題",title)
可以取到標題
}
}
catch(JSONParserExpection e){
Log.e("json","error",e)
}