新的 JSONObject(Map map) 没有给出期望的结果

new JSONObject(Map map) not giving desired result

我需要发送一个POST请求,负载json,要求整个项目是轻量级的,所以只是一个简单的java项目,我' m 使用 java.net.HttpURLConnectionorg.json.JSONObject.

此方法编译我的有效载荷:

public static String compileSRF() throws JSONException{
    Map<String, Boolean> flags = new HashMap<String, Boolean>();
    flags.put("overrideStore", true);
    flags.put("matchmaking", true);

    JSONObject orchestrationFlags = new JSONObject(flags);
    JSONObject requesterSystem = new JSONObject();
    JSONObject requestedService = new JSONObject();

    requesterSystem.put("systemGroup", "testGroup");
    requesterSystem.put("systemName", "testSystem");
    requesterSystem.put("address", "localhost");

    requestedService.put("serviceGroup", "Temperature");
    requestedService.put("serviceDefinition", "IndoorTemperature");
    List<String> interfaces = new ArrayList<String>();
    interfaces.add("json");
    requestedService.put("interfaces", interfaces);

    JSONObject payload = new JSONObject();
    payload.put("requesterSystem", requesterSystem);
    payload.put("requestedService", requestedService);
    payload.put("orchestrationFlags", orchestrationFlags);

    return payload.toString();
}

生成的有效负载如下所示:

{
"orchestrationFlags": {
    "overrideStore": true,
    "matchmaking": true
},
"requesterSystem": {
    "address": "localhost",
    "systemName": "testSystem",
    "systemGroup": "testGroup"
},
"requestedService": {
    "interfaces": ["json"],
    "serviceGroup": "Temperature",
    "serviceDefinition": "IndoorTemperature"
}
}

但是当这个有效载荷到达我的网络服务器时,代码试图解析 "orchestrationFlags" 哈希图,但它没有成功,而是使用默认值。当我在 Web 服务器上对代码进行测试时,这是我一直在 Postman 中使用的有效负载结构并且它有效:

{
"orchestrationFlags": {
     "entry": [
        {
            "key": "overrideStore",
            "value": true
        },
        {
            "key": "matchmaking",
            "value": true
        }
    ]
},
//requesterSystem and requestedService is the same
}

如何使用 JSONObject 实现此目的? (或使用另一个简单的 API,但 maven import 不是一个选项) 谢谢!

Use List<Map<String,Object>> for the entry attribute and  put this value in orchestrationFlags json object.

//Refactored code below:    
public static String compileSRF() throws JSONException{

        List<Map<String,Object>> entryList = new ArrayList<>();
        Map<String, Object> flag1 = new HashMap<>();
        flag1.put("key", "overrideStore");
        flag1.put("value", true);
        entryList.add(flag1);

        Map<String, Object> flag2 = new HashMap<>();
        flag2.put("key", "matchmaking");
        flag2.put("value", true);
        entryList.add(flag2);


        JSONObject orchestrationFlags = new JSONObject();
        JSONObject requesterSystem = new JSONObject();
        JSONObject requestedService = new JSONObject();

        orchestrationFlags.put("entry", entryList);

        requesterSystem.put("systemGroup", "testGroup");
        requesterSystem.put("systemName", "testSystem");
        requesterSystem.put("address", "localhost");

        requestedService.put("serviceGroup", "Temperature");
        requestedService.put("serviceDefinition", "IndoorTemperature");
        List<String> interfaces = new ArrayList<String>();
        interfaces.add("json");
        requestedService.put("interfaces", interfaces);

        JSONObject payload = new JSONObject();
        payload.put("requesterSystem", requesterSystem);
        payload.put("requestedService", requestedService);
        payload.put("orchestrationFlags", orchestrationFlags);

        return payload.toString(4);
    }

Output:
{
    "orchestrationFlags": {"entry": [
        {
            "value": true,
            "key": "overrideStore"
        },
        {
            "value": true,
            "key": "matchmaking"
        }
    ]},
    "requesterSystem": {
        "address": "localhost",
        "systemName": "testSystem",
        "systemGroup": "testGroup"
    },
    "requestedService": {
        "interfaces": ["json"],
        "serviceGroup": "Temperature",
        "serviceDefinition": "IndoorTemperature"
    }
}

Hope this helps.