2016-07-28 39 views
0

有人可以幫我在下面通過在JSON data身體我HTTP POST電話..有人可以幫助我在身體傳遞JSON數據HTTP POST調用

{ 
    "name": "ABC", 
    "user": "fl9f03fe24a2c4a4b51a4d75", 
    "data": 
    { 
     "details": "component", 
     "Key": "123", 
     "region": "server-23" 
    } 

} 
+0

你至今嘗試過什麼?分享你的代碼。 –

+0

你在哪裏做這個POST調用?你有什麼錯誤嗎? –

+0

一個可能的答案是已經在[鏈接](http://stackoverflow.com/questions/7181534/http-post-using-json-in-java) –

回答

0

因此,這是你的代碼(我編輯您的文章請接受它):

JSONObject obj = new JSONObject(); 
obj.put("name", "ABC"); 
obj.put("user","fl9f03fe24a2c4a4b51a4d75"); 
datanew = (JSONObject) obj.get("data"); 
datanew.put("details", "component"); 
datanew.put("Key", "123"); 
datanew.put("region","server-23"); 

你的JSON對象obj是新的,空的,它不包含任何東西。所以你不能get("data")之前你put("data")第一次。試試這個:

JSONObject obj = new JSONObject(); 
obj.put("name", "ABC"); 
obj.put("user","fl9f03fe24a2c4a4b51a4d75"); 

JSONObject datanew = new JSONObject(); 
datanew.put("details", "component"); 
datanew.put("Key", "123"); 
datanew.put("region","server-23"); 

obj.put("data", datanew); 
+0

嘿它的工作感謝:) –