0
Map < String, Map < String, String >> appPayload = new HashMap < String, Map < String, String >> ();
Map < String, String > featurePayload = new HashMap < String, String > ();

JSONArray appList = new JSONArray();            
JSONArray requestDetails = new JSONArray();
requestDetails = new JSONObject(payload).getJSONArray("requestDetails");     
appList = requestDetails.getJSONObject(0).getJSONArray("apps");         

for (int i =0; i<appList.length();i++) {

    String appName = appList.getJSONObject(i).getString("appName");
    JSONArray featureList = appList.getJSONObject(i).getJSONArray("features");              

    for (int j =0; j<featureList.length();j++) {
        String featureName = featureList.getJSONObject(j).getString("featName");
        String featureResponse = featureList.getJSONObject(j).getString("featResponse");
        featurePayload.put(featureName, featureResponse);
    }
    appPayload.put(appName, featurePayload);
}

return appPayload;

}

Output :

Appname : CD

{Video Snapshot=enabled, Base=enabled}

Appname : CSU
{Video Snapshot=enabled, Base=enabled}

Appname : FO
{Video Snapshot=enabled, Base=disabled}

Appname : EDL
{Video Snapshot=enabled, Base=enabled}

Appname : CA
{Video Snapshot=enabled, Base=disabled}

And here is the json string

{
  "apiVersion": "1.0",
  "providerID": 504070500,
  "timeStamp": "2019-09-12T19:51:56.902Z",
  "messageID": "3abb70f4-d5bb-11e9-bb65-2a2ae2dbcce4",
  "requestDetails": [
    {
      "esn": "64000008",
      "boxID": "357649070551015",
      "apps": [
        {
          "appName": "CD",
          "features": [
            {
              "featName": "Base",
              "featResponse": "enabled"
            },
            {
              "featName": "Video Snapshot",
              "featResponse": "enabled"
            }
          ]
        },
        {
          "appName": "CSU",
          "features": [
            {
              "featName": "Base",
              "featResponse": "enabled"
            }
          ]
        },
        {
          "appName": "FO",
          "features": [
            {
              "featName": "Base",
              "featResponse": "disabled"
            }
          ]
        },
        {
          "appName": "EDL",
          "features": [
            {
              "featName": "Base",
              "featResponse": "enabled"
            }
          ]
        },
        {
          "appName": "CA",
          "features": [
            {
              "featName": "Base",
              "featResponse": "disabled"
            }
          ]
        }
      ]
    }
  ]
}
azro
  • 53,056
  • 7
  • 34
  • 70
Neeraj
  • 11
  • 1
  • 2

1 Answers1

0

Try to instantiate featurePayload before the nested loop

Ryan19
  • 71
  • 4
  • Thanks! It worked. What changes when I instantiate it before the nested loop. Please educate me! :D – Neeraj Oct 16 '19 at 14:12
  • All the values of app payload were referencing the same featurePayload HashMap, which is getting updated each iteration. So your end result would be, appPayload map with each app having the same value. Remember, Objects are passed through reference in java.. – Ryan19 Oct 16 '19 at 15:10