2017-09-27 54 views
0

改造2我將如何POST使用具有內部json的地圖json體。讓我告訴你一個例子:改造地圖爲身體 - 如何張貼與內部json對象的地圖

{ 
    "id_invoice": 1234, 
    "action": "viewed", 
    "data": { 
     "id_object": 88, 
     "id_store": 43, 
     "type": "payment" 
    } 
} 

公告「數據」 JSON對象如何具有內部JSON。我怎樣才能放入地圖這一切的話,我可以把它使用以下服務端點改造:

public interface Api { 
    @NonNull 
    @POST("cart/payment") 
    @Headers({"Content-Type:application/json"}) 
    Observable<ResponseBody> postPaymentEvent(@Body Map<String, Object> body); 
} 

我試過如下:

Map<String, Object> map = new HashMap<>(); 
JSONObject json = new JSONObject(); 
json.put("id_object", 88); 
json.put("id_store", 43); 
json.put("type", "payment"); 
map.put("action", "view"); 
map.put("id_invoice", 1234); 
map.put("data", json); //this is wrong. it creates the following response with a nameValuePairs field, which is not what i want: 

身體:

{ 
    "action": "page_view", 
    "id_invoice": "1234", 
    "data": { 
     "nameValuePairs": { 
      "id_object": 88, 
      "id_store": 43, 
      "type": "view" 
     } 
    } 
} 

回答

0

這樣做:

JSONObject dataJson = new JSONObject(); 
      try { 
       JSONObject parentJson = new JSONObject(); 

       dataJson.accumulate("id_object", 88); 
       dataJson.accumulate("id_store", 43); 
       dataJson.accumulate("type", "payment"); 


       parentJson.accumulate("id_invoice",1234); 
       parentJson.accumulate("action","viewed"); 
       parentJson.put("data",dataJson); 



       Log.d("Main Screen ",parentJson.toString()); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

它會產生這樣的:

{ 
    "id_invoice": 1234, 
    "action": "viewed", 
    "data": { 
    "id_object": 88, 
    "id_store": 43, 
    "type": "payment" 
    } 
} 
+0

我需要一個HashMap中。 – j2emanue

+0

你是否在散列圖中添加了其他東西? –

+0

不,我只需要將該json放入地圖中即可。顯然,我可以使用一個類和gson。但是試圖與地圖 – j2emanue