0 0

Android json解析问题30

这样的的格式的json数据怎么解析呢

[{"id":"24","bigtype":"生命","image":"1.jpg"},{"id":"23","bigtype":"生活","image":"2.png"},{"id":"22","bigtype":"河健康","image":"3.png"},{"id":"21","bigtype":"财运","image":"4.png"},{"id":"20","bigtype":"girl","image":"5.png"},{"id":"19","bigtype":"好运","image":"6.png"}]


问题补充:
liuqing_2010_07 写道
这是一个list吧!


就是一个从服务器上面读取的一个json格式的数据啊,不知道怎么去解析

问题补充:String str ="[{"id":"24","bigtype":"生命","image":"1.jpg"},{"id":"23","bigtype":"生活","image":"2.png"},{"id":"22","bigtype":"河健康","image":"3.png"},{"id":"21","bigtype":"财运","image":"4.png"},{"id":"20","bigtype":"girl","image":"5.png"},{"id":"19","bigtype":"好运","image":"6.png"}] " ;

JSONObject jsonObject = new JSONObject(str);

在执行时就报错01-08 13:18:41.183: E/AndroidRuntime(19253): Caused by: org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject  不嫩从String转换成jsonObject

到底怎么回事啊?

问题补充:还是不行啊,还是会在执行
JSONObject jsonObject = new JSONObject(str);
报类型转换的异常

问题补充:
renpeng301 写道
你返回的json字符串可能要转换下

String jsonstr="[color=red][b] {List:   [/b][/color]
\"[{\"id\":\"24\",\"bigtype\":\"生命\",\"image\":\"1.jpg\"},{\"id\":\"23\",\"bigtype\":\"生活\",\"image\":\"2.png\"},{\"id\":\"22\",\"bigtype\":\"河健康\",\"image\":\"3.png\"},{\"id\":\"21\",\"bigtype\":\"财运\",\"image\":\"4.png\"},{\"id\":\"20\",\"bigtype\":\"girl\",\"image\":\"5.png\"},{\"id\":\"19\",\"bigtype\":\"好运\",\"image\":\"6.png\"}] [color=red]}[/color]  
";


 JSONObject jsonObject = new JSONObject(jsonstr.toString()); 
JSONArray arrayJson = jsonObject .getJSONArray("List");

for(int i=0;i<arrayJson.length();i++) {
71	                 
72	                JSONObject tempJson = arrayJson.optJSONObject(i);
73	                 
74	                Log.d(TAG, tempJson.getString("id"));
75	                Log.d(TAG, tempJson.getString("bigtype"));
76	                Log.d(TAG, tempJson.getString("image"));  
77	            }


是不是我从服务器上面读取的数据不符合格式啊
2012年1月07日 20:47

16个答案 按时间排序 按投票排序

0 0

可以考虑使用传递xml文件格式的时候 为了提高传递效率 可以考虑使用gzip压缩  再使用android自动的pull进行解析 用着非常不错呀

2012年1月10日 13:05
0 0

数组里的元素数据才可以转成json对象,而不是整个数组

2012年1月09日 13:47
0 0

import org.json.*;


  String json = "[{\"id\":\"24\",\"bigtype\":\"生命\",\"image\":\"1.jpg\"},{\"id\":\"23\",\"bigtype\":\"生活\",\"image\":\"2.png\"},{\"id\":\"22\",\"bigtype\":\"河健康\",\"image\":\"3.png\"}, {\"id\":\"21\",\"bigtype\":\"财运\",\"image\":\"4.png\"}, {\"id\":\"20\",\"bigtype\":\"girl\",\"image\":\"5.png\"},{\"id\":\"19\",\"bigtype\":\"好运\",\"image\":\"6.png\"}] ";
       
        try {
JSONArray  jsonArray = new JSONArray(json);
int arrayLength = jsonArray.length();
final String[] keys =  { "id", "bigtype", "image"};
for(int index = 0; index < arrayLength; index++) {
JSONObject  jsonObject = (JSONObject) jsonArray.get(index);
for(String key: keys) {
Log.d(key, jsonObject.getString(key));
}
}

} catch (JSONException e) {
e.printStackTrace();
}

2012年1月09日 12:28
0 0

你用第三方的json-lib包吧
要么就有gjson

android自带的json库 很弱啊啊

2012年1月09日 11:40
0 0

不是 你这个是 list to json 生成的数据吧·
那你这样吧
JSONObject jsonObject = JSONObject.fromObject(jsonstr); 

List<TestBean> list = new ArrayList<TestBean>(); 

for (Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
list.add((TestBean)JSONObject.toBean(jsonObject.getJSONObject(iterator.next().toString()), TestBean.class));
} 






public class TestBean{
 String id;
 public String getId() {
	return id;
}
public void setId(String id) {
	this.id = id;
}
public String getBigtype() {
	return bigtype;
}
public void setBigtype(String bigtype) {
	this.bigtype = bigtype;
}
public String getImage() {
	return image;
}
public void setImage(String image) {
	this.image = image;
}
String bigtype;
 String image;
}

2012年1月08日 18:10
0 0

你返回的json字符串可能要转换下

String jsonstr="[color=red][b] {List:   [/b][/color]
\"[{\"id\":\"24\",\"bigtype\":\"生命\",\"image\":\"1.jpg\"},{\"id\":\"23\",\"bigtype\":\"生活\",\"image\":\"2.png\"},{\"id\":\"22\",\"bigtype\":\"河健康\",\"image\":\"3.png\"},{\"id\":\"21\",\"bigtype\":\"财运\",\"image\":\"4.png\"},{\"id\":\"20\",\"bigtype\":\"girl\",\"image\":\"5.png\"},{\"id\":\"19\",\"bigtype\":\"好运\",\"image\":\"6.png\"}] [color=red]}[/color]  
";


 JSONObject jsonObject = new JSONObject(jsonstr.toString()); 
JSONArray arrayJson = jsonObject .getJSONArray("List");

for(int i=0;i<arrayJson.length();i++) {
71	                 
72	                JSONObject tempJson = arrayJson.optJSONObject(i);
73	                 
74	                Log.d(TAG, tempJson.getString("id"));
75	                Log.d(TAG, tempJson.getString("bigtype"));
76	                Log.d(TAG, tempJson.getString("image"));  
77	            }

2012年1月08日 15:00
0 0

"   
\"[{\"id\":\"24\",\"bigtype\":\"生命\",\"image\":\"1.jpg\"},{\"id\":\"23\",\"bigtype\":\"生活\",\"image\":\"2.png\"},{\"id\":\"22\",\"bigtype\":\"河健康\",\"image\":\"3.png\"},{\"id\":\"21\",\"bigtype\":\"财运\",\"image\":\"4.png\"},{\"id\":\"20\",\"bigtype\":\"girl\",\"image\":\"5.png\"},{\"id\":\"19\",\"bigtype\":\"好运\",\"image\":\"6.png\"}]   
"; 


你双引号 没有转义  你看看我给你发的字符串 。

2012年1月08日 13:31
0 0

上面的Gson下载地址 时而不好使 ,但是能够下载。多试几次。
现在给你几个例子:
http://www.cnblogs.com/lupeng0527/archive/2011/08/06/2129547.html

2012年1月08日 09:35
0 0

在adnroid上解析json
谷歌得
Google Gson(gson-xxx.jar)下载地址:http://code.google.com/p/google-gson/downloads/list
是很好得选择。。。

2012年1月08日 09:02
0 0

String jsonstr= "  
\"[{\"id\":\"24\",\"bigtype\":\"生命\",\"image\":\"1.jpg\"},{\"id\":\"23\",\"bigtype\":\"生活\",\"image\":\"2.png\"},{\"id\":\"22\",\"bigtype\":\"河健康\",\"image\":\"3.png\"},{\"id\":\"21\",\"bigtype\":\"财运\",\"image\":\"4.png\"},{\"id\":\"20\",\"bigtype\":\"girl\",\"image\":\"5.png\"},{\"id\":\"19\",\"bigtype\":\"好运\",\"image\":\"6.png\"}]  
";  

     JSONObject jsonObject = new JSONObject(jsonstr.toString()); 
   String id = jsonObject .get("id").toString(); 
   String bigtype= jsonObject .get("bigtype").toString(); 
   String image= jsonObject .get("image").toString(); 
                           

2012年1月08日 08:13
0 0

前台一样的 !你看看 就明白了 。呵呵呵。。。。你懂得!!!!

2012年1月07日 21:16
0 0
0 0

引用

就是一个从服务器上面读取的一个json格式的数据啊,不知道怎么去解析


我的天这个是在手机前台解释  我那白写了。。。。。呜呜。。。。。。。

2012年1月07日 21:11
0 0

在前台解释?还是在后台解释 ?上面我写的是在后台解释。。。。

2012年1月07日 21:10
0 0

public class MyBean {
 String id;
 public String getId() {
	return id;
}
public void setId(String id) {
	this.id = id;
}
public String getBigtype() {
	return bigtype;
}
public void setBigtype(String bigtype) {
	this.bigtype = bigtype;
}
public String getImage() {
	return image;
}
public void setImage(String image) {
	this.image = image;
}
String bigtype;
 String image;
}

public Object getMyBean(Class pojoCalss) {
String res = "
\"[{\"id\":\"24\",\"bigtype\":\"生命\",\"image\":\"1.jpg\"},{\"id\":\"23\",\"bigtype\":\"生活\",\"image\":\"2.png\"},{\"id\":\"22\",\"bigtype\":\"河健康\",\"image\":\"3.png\"},{\"id\":\"21\",\"bigtype\":\"财运\",\"image\":\"4.png\"},{\"id\":\"20\",\"bigtype\":\"girl\",\"image\":\"5.png\"},{\"id\":\"19\",\"bigtype\":\"好运\",\"image\":\"6.png\"}]
";
JSONArray jsonArray = JSONArray.fromObject(jsonString);
JSONObject jsonObject;
Object pojoValue;
List list = new ArrayList();
for ( int i = 0 ; ijsonArray.size(); i++){
jsonObject = jsonArray.getJSONObject(i);
pojoValue = JSONObject.toBean(jsonObject,pojoClass);
list.add(pojoValue);
}
return list;

}


你看看 这个行不行。

2012年1月07日 21:09
0 0

这是一个list吧!

2012年1月07日 20:51

相关推荐

Global site tag (gtag.js) - Google Analytics