首页 > android中如何解析没有Key的Json数据

android中如何解析没有Key的Json数据

各位,我从服务器上得到返回的JSON数据没有KEY该如何解析啊?先谢谢各位了。

像是下面这样的JSON数据,

[
     {
        "screen_name": "阿信",
         "followers_count": 11091994,
         "uid": 1265020392
     },
     {
         "screen_name": "阿信星闻",
         "followers_count": 22427,
         "uid": 1822541757
     }

 ]

前都没有key值的,该怎么解析啊,我试着先将字符串转化为数组,在循环遍历数组提取出每个数组中想要的:

JSONArray jsonArray = new JSONArray(retStr);
for (int i = 0; i < jsonArray.length(); i++)
{
  JSONObject jsonObject = jsonArray.getJSONObject(i);
	{
	  HashMap<String, Object> hashMap = new HashMap<String, Object>();
	  hashMap.put("count", jsonObject.getString("followers_count"));
	  hashMap.put("name", jsonObject.getString("screen_name"));
	  Data.add(hashMap);			          
         }
}

但是提示我 org.json.JSONException: Value <html of type java.lang.String cannot be converted to JSONArray 那怎么弄啊?


List newList=new;
newList = new ArrayList();
try{
JSONArray jsonArray = new JSONArray(arg2); //获取到的String转换为JsonArray

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i); // 得到每个对象
News news=new News(); //新建一个类 类的字段 对应每个对象上的key(字段数量不限)
news.setId(item.getString("id")); //id 就是数组上每个对象内的key
news.setCode(item.getString("code"));
news.setName(item.getString("name"));
news.setCreatedTime(item.getString("createdTime"));
newList.add(news); //newList 是一个数组 类型为
}


import org.json.JSONException;
import org.json.JSONObject;

//先把字符串转化为数组,然后遍历数组,在提取

JSONObject jstr = null;
try {
jstr = new JSONObject(arg1);

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

String game_serverid = null;
try {
game_serverid = jstr.getString("game_serverid");
} catch (JSONException e) {
e.printStackTrace();
}

String game_userid = null;
try {
game_userid = jstr.getString("game_userid");
} catch (JSONException e) {
e.printStackTrace();
}

方法1:割断字符串
删除前后的中括号,将{}提取出来(匹配"},{"来找到分界点,然后割断字符窜,行成一个数组,这些数组里面添加就是json),里面是有key:value的,

方法2:插入法
在每一个大段没有key的{前,插入自己的key,可以是这样:[1:{...},2:{...}, n:{...}],然后在用我上面给的方法。

这两种方法仅限于你这类比较简单的json的。


jsonObject.optString("screen_name","")即可;


jsonObject = new JSONObject(contents.trim());
Iterator<?> keys = jsonObject.keys();

while( keys.hasNext() ){
    String key = (String)keys.next();
    if( jsonObject.get(key) instanceof JSONObject ){

    }
}

http://stackoverflow.com/questions/9151619/java-iterate-over-jsonobject/10593838#10593838


JSONObject job=...
JSONArray jarr = job.names();

试试看


你可以看一下数据,你返回数据是不正确的的。应该是服务器端出错了,返回了错误提示,要么就是你地址用错了。
它都提示你<html 了,很明示这是html的标签,说明返回的数据就是个标准的网页,根本就不是JSON。

【热门文章】
【热门文章】