0
我需要发送一个POST请求与JSON有效载荷,这是一个要求,整个项目是轻量级的,所以它只是一个简单的Java项目,我使用java.net.HttpURLConnection
和org.json.JSONObject
。新的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"
}
}
但是,当这种负载获取到我的web服务器,代码试图解析“orchestrationFlags “hashmap,它不会成功,而是使用默认值。当我做Web服务器上的代码测试,这是我一直在使用邮差荷载结构和它的工作:
{
"orchestrationFlags": {
"entry": [
{
"key": "overrideStore",
"value": true
},
{
"key": "matchmaking",
"value": true
}
]
},
//requesterSystem and requestedService is the same
}
我怎样才能实现这一目标用的JSONObject? (或与另一个简单的API,但maven导入不是一种选择) 谢谢!