2016-08-16 8 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" 
} 
} 

しかし、このペイロードは、私のWebサーバに取得し、コードが「orchestrationFlagsを解析しようとするとき"hashmap、成功しません、代わりにデフォルト値を使用します。私は、Webサーバー上のコードのテストをしたときは、これは、ペイロード構造である私はいつも郵便配達に使ってきたし、それが働いた:

{ 
"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. 
関連する問題