【问题标题】:Get json array keys in android在android中获取json数组键
【发布时间】:2013-12-02 05:03:34
【问题描述】:
{
    "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"
    }
}

以上示例json数组,如何获取204、203、202?谢谢

我试过了:

JSONArray issueArray = new JSONArray(jsonContent);

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

【问题讨论】:

  • 唯一的关键你想要对的家伙..
  • 这不是一个json数组,json数组从“[”括号不是“{”这个开始,首先将您的数组格式化为有效的json数组并编辑您的问题

标签: android json


【解决方案1】:

以上示例json数组,如何获取204、203、202?

不,当前字符串是 JSONObject 而不是 JSONArray。如果内部 JSONObject 键是动态的,则应该使用 JSONObject. keys () 获取迭代器:

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");
    }

【讨论】:

  • 很好的答案,它展示了如何从嵌套的 json 中获取值...
【解决方案2】:

K先生的回答也是对的,但是你也可以使用jsonObject names()方法。请找到示例代码

for(int i = 0; i<jsonobject.length(); i++){
    Log.e(TAG, "Key = " + jsonobject.names().getString(i) + " value = " + jsonobject.get(jsonobject.names().getString(i)));
}

希望对你有帮助

【讨论】:

    【解决方案3】:

    使用此方法动态迭代json

    private void parseJson(JSONObject data) {
    
            if (data != null) {
                Iterator<String> it = data.keys();
                while (it.hasNext()) {
                    String key = it.next();
    
                    try {
                        if (data.get(key) instanceof JSONArray) {
                            JSONArray arry = data.getJSONArray(key);
                            int size = arry.length();
                            for (int i = 0; i < size; i++) {
                                parseJson(arry.getJSONObject(i));
                            }
                        } else if (data.get(key) instanceof JSONObject) {
                            parseJson(data.getJSONObject(key));
                        } else {
                            System.out.println("" + key + " : " + data.optString(key));
                        }
                    } catch (Throwable e) {
                        System.out.println("" + key + " : " + data.optString(key));
                        e.printStackTrace();
    
                    }
                }
            }
        }
    

    【讨论】:

      【解决方案4】:

      您也可以使用names() 来获取JSONArray 的密钥:

      JSONArray jArray = jsonObject.names();
      int len = jsonObject.length();
      for (int i=0; i<len; i++) {
          String keyName = (String)jArray.get(i);
          JSONObject jValue = jsonObject.getJSONObject(keyName);
          String _pubKey = jValue.optString("id");
          //get the other values from jValue
      }
      

      【讨论】:

        【解决方案5】:

        在这里,您尝试获取 JSONArray,但在 json 响应中它是 JSONObject

        使用以下代码。

        JSONObject issueObject = new JSONObject(jsonContent);
        String _pubKey = issueObject.getString(0);
        

        【讨论】:

          猜你喜欢
          • 2016-06-22
          • 2015-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-25
          • 1970-01-01
          • 2019-02-28
          相关资源
          最近更新 更多