0
{
   "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();
    }
Yougandhara
  • 329
  • 1
  • 4
  • 15
  • you can convert JSON to java POJO, then from there, you can convert to map/list. – dkb Apr 15 '21 at 04:01
  • @dkb Could you please suggest the parser approach other than pojo. Since creating POJO would be costly depending on no of different types of responses I'll receive from the service. – Yougandhara Apr 15 '21 at 04:31
  • you can have a union of all the responses and create one POJO with ignoring null type check. Also, If you know the cost of creating POJO, please let me know. – dkb Apr 15 '21 at 04:45
  • Can I use the JSON Parser or the JsonArray, JsonObject to iterate over it instead of POJO class – Yougandhara Apr 15 '21 at 05:14
  • you can, but you need to know JSON structure before you write your parsing logic. In the end, you are doing the same as changing it to POJO. – dkb Apr 15 '21 at 05:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231156/discussion-between-yougandhara-and-dkb). – Yougandhara Apr 15 '21 at 05:26

0 Answers0