Question Detail

How can I access each key and value in JSONArray in java

6 years ago Views 1427 Visit Post Reply

I have a service where is some data coming according to day. if a day has any transaction then it will reflect or listed in JsonArray else Not show that day in the list.
So Here I need to fetch key and Values both dynamically. I can get Values but I have tried may thing for this but not succeed please Help How can I resolve this Issue? 


Thread Reply

Hemant Sharma

- 6 years ago

I also stuck on this issue I have tried this 

JSONArray rateJsonObj = jsonObject.getJSONArray("[{\"Monday\": \"12\"\n }, {\n\"Tuesday\": \"1\"\n }, {\n \"Wednesday\": \"3\"\n }, {\n \"Friday\": \"3\"\n }, {\n \"Saturday\": \"3\"\n }]");
for (int i = 0; i < rateJsonObj.length(); i++) {
    JSONObject json = rateJsonObj.getJSONObject(i);
    Iterator<String> keys = json.keys();

    while (keys.hasNext()) {
        String key = keys.next();
        System.out.println("Key :" + key + "  Value :" + json.get(key));
    }

}


this is the Right way to resolve your Issue.