0

I have been searching from a long time and no solutions is working for me. I have to retrieve the values from the json object using some loop , and number of nested values is random these can be 1 or may be 10. json looks like this :

{
   "keyInfo":[
      {
         "name":"ipek",
         "key":"1221"
      },
      {
         "name":"ipek",
         "key":"1221"
      }
   ],
   "terminalInfo":{
      "dateExp":"2-2-2",
      "deviceId":"1222",
      "tid":"122"
   }
}

I have tried alot of solutions one of them is this :

 JSONObject jsonObject =new JSONObject(jsonString);
 JSONObject jsonChildObject = (JSONObject)jsonObject.get("keyInfo");
 Iterator iterator  = jsonChildObject.keys();
 String key = null;
 while(iterator.hasNext()){
     key = (String)iterator.next();
     System.out.println("inval value: "
         + ((JSONObject)jsonChildObject.get(key)).get("inval"));
 }

but none of them is working for me please help. THANKS IN ADVANCE.

A4L
  • 17,353
  • 6
  • 49
  • 70
Talib
  • 1,134
  • 5
  • 31
  • 58

1 Answers1

0

You are trying to use an array as a map. jsonChildObject is actually an JSONArray. It does not have keys; for instance you can have the same {"name":"ipek","key":"1221"} multiple times. If you know that the key is unique among items in this array, you can try and build a HashMap out of it if you need, but the structure you have is definitely an array.

Andrei Tudor Diaconu
  • 2,157
  • 21
  • 26