16
{
    "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);
}
2
  • only key u want right dude.. Commented Dec 2, 2013 at 5:09
  • This is not a json array,json array Start from "[" bracket not "{" this one,first format your array to valid json array & edit your question Commented Dec 2, 2013 at 6:37

5 Answers 5

33

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");
    }
Sign up to request clarification or add additional context in comments.

1 Comment

good answer, it shows how to get the values from nested json...
12

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

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

Comments

11

User this method to iterate json dynamically

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

                }
            }
        }
    }

Comments

0

You can also use names() as below to get the keys as 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
}

Comments

-1

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

Use following code.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.