获取Android的JSON数组键 [英] Get json array keys in android

查看:158
本文介绍了获取Android的JSON数组键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
    204:{
        主持人:HTTPS:\ / \ / abc.com \ /生产源\ /长沙\ / 2013 \ / 12 \ / 02 \ / 0 \ / 0 \ / A \ /内容\ /,
        时间戳:1385909880,
        封面:17 \ /Pg017.png
        18 \ /Pg018.png
        1 \ /Pg001.png
        2 \ /Pg002.png],
        年:2013​​,
        月:12,
        天:02,
        问题:2013年12月2日,
        ID:204
    },
    203:{
        主持人:HTTPS:\ / \ / abc.com \ /生产源\ /长沙\ / 2013 \ / 12 \ / 01 \ / 0 \ / 0 \ / A \ /内容\ /,
        时间戳:1385806902,
        封面:1 \ /Pg001.png
        2 \ /Pg002.png
        3 \ /Pg003.png
        4 \ /Pg004.png],
        年:2013​​,
        月:12,
        天:01,
        问题:二〇一三年十二月一日,
        ID:203
    },
    202:{
        主持人:HTTPS:\ / \ / abc.com \ /生产源\ /长沙\ / 2013 \ / 11 \ / 30 \ / 0 \ / 0 \ / A \ /内容\ /,
        时间戳:1385720451,
        封面:1 \ /Pg001.png
        2 \ /Pg002.png
        3 \ /Pg003.png
        4 \ /Pg004.png],
        年:2013​​,
        月:11,
        天:30,
        问题:2013年11月30号,
        ID:202
    }
}
 

以上示例JSON数组,如何得到204,203和202?谢谢

我想:

  JSONArray issueArray =新JSONArray(jsonContent);

对于(INT J = 0; J< issueArray.length(); J ++){
    JSONObject的问题= issueArray.getJSONObject(J);
    串_pubKey = issue.getString(0);
}
 

解决方案
  

样品JSON数组上面,如何让204,203和202?

没有,当前String是的JSONObject 而不是 JSONArray 。你应该使用的JSONObject获得迭代器。键()如果内部的JSONObject密钥动态的:

 的JSONObject issueObj =新的JSONObject(jsonContent);
迭代器迭代器= issueObj.keys();
   而(iterator.hasNext()){
    字符串键=(字符串)iterator.next();
    JSONObject的问题= issueObj.getJSONObject(密钥);

    //从发行ID
        字符串_pubKey = issue.optString(ID);
    }
 

{
    "204": {
        "host": "https:\/\/abc.com\/production-source\/ChangSha\/2013\/12\/02\/0\/0\/A\/Content\/",
        "timestamp": 1385909880,
        "cover": ["17\/Pg017.png",
        "18\/Pg018.png",
        "1\/Pg001.png",
        "2\/Pg002.png"],
        "year": "2013",
        "month": "12",
        "day": "02",
        "issue": "2013-12-02",
        "id": "204"
    },
    "203": {
        "host": "https:\/\/abc.com\/production-source\/ChangSha\/2013\/12\/01\/0\/0\/A\/Content\/",
        "timestamp": 1385806902,
        "cover": ["1\/Pg001.png",
        "2\/Pg002.png",
        "3\/Pg003.png",
        "4\/Pg004.png"],
        "year": "2013",
        "month": "12",
        "day": "01",
        "issue": "2013-12-01",
        "id": "203"
    },
    "202": {
        "host": "https:\/\/abc.com\/production-source\/ChangSha\/2013\/11\/30\/0\/0\/A\/Content\/",
        "timestamp": 1385720451,
        "cover": ["1\/Pg001.png",
        "2\/Pg002.png",
        "3\/Pg003.png",
        "4\/Pg004.png"],
        "year": "2013",
        "month": "11",
        "day": "30",
        "issue": "2013-11-30",
        "id": "202"
    }
}

The above sample json array , how to get the 204, 203 and 202? Thanks

I tried:

JSONArray issueArray = new JSONArray(jsonContent);

for (int j = 0; j < issueArray.length(); j++) {
    JSONObject issue = issueArray.getJSONObject(j);
    String _pubKey = issue.getString(0);
}

解决方案

above sample json array , how to get the 204, 203 and 202?

No, current String is JSONObject instead of JSONArray. you should get Iterator using JSONObject. keys () if inner JSONObject keys dynamic as:

JSONObject issueObj = new JSONObject(jsonContent);
Iterator iterator = issueObj.keys();
   while(iterator.hasNext()){
    String key = (String)iterator.next();
    JSONObject issue = issueObj.getJSONObject(key);

    //  get id from  issue
        String _pubKey = issue.optString("id");
    }

这篇关于获取Android的JSON数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆