2016-08-16 89 views
0

我需要发送一个POST请求与JSON有效载荷,这是一个要求,整个项目是轻量级的,所以它只是一个简单的Java项目,我使用java.net.HttpURLConnectionorg.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导入不是一种选择) 谢谢!

回答

1
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. 
相关问题