2016-08-16 5 views
0

json 페이로드로 POST 요청을 보내야합니다. 전체 프로젝트가 가볍기 때문에 간단한 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" 
} 
} 

을하지만이 페이로드 내 웹 서버에 도달하고, 코드는 "orchestrationFlags을 구문 분석 할 때 "hashmap, 성공하지 못하고 대신 기본값을 사용합니다. 나는 웹 서버의 코드에 대한 테스트를했을 때, 이것은 내가 항상 우편 배달에 사용했던 페이로드 구조이며 일 :

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

가 어떻게 된 JSONObject와이 achive 수 있습니까? (또는 다른 간단한 API를 사용하지만 가져 오기는 옵션이 아닙니다.) 감사합니다!

답변

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.