在android中获取json数组键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20321212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:38:20  来源:igfitidea点击:

Get json array keys in android

androidjson

提问by user782104

{
    "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

上面的示例json数组,如何得到204、203和202?谢谢

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

回答by ρяσ?ρ?я K

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

上面的示例 json 数组,如何获得 204、203 和 202?

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

不,当前 StringJSONObject不是JSONArray. 您应该使用JSONObject获取迭代器keys ()如果内部 JSONObject 键动态为:

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

回答by Amandeep Rohila

Answer given by Mr. K is also right but you can also use jsonObject names()method. please find the sample code

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

I hope dis will help you

希望dis能帮到你

回答by Biraj Zalavadia

User this method to iterate json dynamically

使用此方法动态迭代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();

                }
            }
        }
    }

回答by Nongthonbam Tonthoi

You can also use names()as below to get the keys as JSONArray:

您还可以使用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
}

回答by Jitesh Dalsaniya

Here you are trying to get JSONArraybut in json response it is JSONObject.

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

Use following code.

使用以下代码。

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