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;

}

输出 :

应用名称: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}

这是json字符串

{
  "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"
            }
          ]
        }
      ]
    }
  ]
}
4

1 回答 1

0

尝试在嵌套循环之前实例化 featurePayload

于 2019-10-15T21:33:40.207 回答