Iterate over Json response with Multiple JSONArray And JSONObject

491 views Asked by At
{
   "Response":"success",
   "errorText":null,
   "TotalBooks":null,
   "privateBookStore":{
      "privateBooks":[
         {
            "Shopid":76354,
            "Shopname":"xyz",
            "Term":null,
            "“Strategy":[
               {
                  "Name":"ABC",
                  "ID":9
               }
            ],
            "magaerTYpe":"Single",
            "Freq":"”monthly”",
            "StructureType":{
               "Name":"ABC",
               "ID":9
            },
            "Currency":[
               "USD",
               "EURO",
               "RUPYEE"
            ]
         }
      ]
   }
}

I have tried with JsonObject and JsonArray but the format of response is containing multiple JsonArray and JsonObject Together

I tried using HashMap and List but its not working for me. Loop through multiple JsonObject I have gone though this too. Kindly suggest the approach

What I have so far is -

inputstream in = new bufferedinputstream(urlconnection.getinputstream());
String result = ioutils.tostring(in utf-8 );
JSONObject  outer = new JSONObject(result);
JSONObject  inner = outer.getJSONObject("privateBookStore");
JSONArray arr = inner.getJSONArray("privateBooks");

also tried with this too but not giving me key:value pair structure

List<HashMap<String,String>> response = new ArrayList<>();
JSONArray jsonarray = null;
    try {
        jsonarray = new JSONArray(json);
        for(int i=0;i<jsonarray.length();i++) {
            JSONObject jsonObject = jsonarray.getJSONObject(i);
            Iterator<?> iterator = jsonObject.keys();
            HashMap<String,String> map = new HashMap<>();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = jsonObject.get(key.toString());
                map.put(key.toString(),value.toString());

            }
            response.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
0

There are 0 answers