问题描述
- android json数组怎解析,求前辈们指导一下。很急,在线等。
- {
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)
}
解决方案二:
用jsonobject或者谷歌的gson插件直接转为类实例
解决方案三:
http://blog.csdn.net/tianjf0514/article/details/7570607
解决方案四:
首先你自己要编写实体类,用jsonobject解析就行
如:public static ArrayList getPersons(String keyString jsonString){
ArrayList list=new ArrayList();
try {
JSONObject jsonObject=new JSONObject(jsonString);
JSONArray jsonArray=jsonObject.getJSONArray(key);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject2=jsonArray.getJSONObject(i);
Person person=new Person();
person.setEmail(jsonObject2.getString(""email""));
person.setUsername(jsonObject2.getString(""username""));
person.setMessage(jsonObject2.getString(""message""));
person.setFigure(jsonObject2.getString(""figure""));
person.setTime(jsonObject2.getString(""time""));
list.add(person);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return list;
}
解决方案五:
服务器传递过来的数据吗?用asynhttp处理,重写Jsonhttpresponse的onsuccess方法