-4

I am a beginner in json programming. I've searched a lot but could not find a suitable solution to get the dynamic key of a JSON output. So i am asking this question. I have a jsonObject as below:

jsonObj = {"A":"A","C":"C","D":"D","B ":"B"}

All the keys are dynamic. I've got many solution where values were retrieved by jsonObj.getString("keyname");But in my case as the keys are all dynamic so i can not put a specific key name in jsonObj.getString("keyname"). How to get all the values as keyname = keyvalue? I am using java spring framework. suppose my json is: {"Tea": "Sugar", "Milj ": "jikk", "Paqjj": "qouta", "Coffee": "Melt"}But i got this output:keys in the json are Sugar My expected output is :key is Tea and value of Tea is Sugar.

1 Answers1

2

Try the below code to get the keys and values :

 JSONObject response = new JSONObject(responseStr)
    Iterator<String> keys = response.keys();
        while (keys.hasNext()) {
              String key = keys.next();
              String value = response.getString(key);
       }
Raju Sharma
  • 2,496
  • 3
  • 23
  • 41