2014-11-03 86 views
-1

我需要帮助发送JSON到服务器端。这是它应该如何看:发送字符串[]作为JSON

"myProfile": { "languages": [ "English", "German" ] } 

所以我的资料是包含“语言”,这是字符串数组一个JSONObject,对不对? 有人可以帮我发送JSON到服务器吗?

JSONObject myProfileObject= new JSONObject(); 
JSONObject languagesObject = new JSONObject(); 

String[] languagesToServer = {"English", "German"}; 
languagesObject.put("languages", languagesToServer); 
myProfileObject.put("myProfile", languagesObject); 

这是创造"myProfile": {"languages":"[Ljava.lang.String;@42b82168"}这显然不好。

有人可以指导我吗?

回答

1
JSONArray mJsonArray = new JSONArray(); 
    mJsonArray.put("English"); 
    mJsonArray.put("German"); 

    JSONObject mJsonObject = new JSONObject(); 
    mJsonObject.put("languages", mJsonArray); 

    JSONObject mObject = new JSONObject(); 
    mObject.put("myProfile", mJsonObject); 

    System.out.println(mObject.toString()); 
+0

谢谢,不知道为什么你是downvoted - 它的作品! – 2014-11-03 19:51:29