2017-08-29 4 views
1

私はこれは、JSONのpbject JSONObject jsonObject =新しいJSONObject()のために、私が持っているコードでPOSTAndroid用のJSONに階層を含めるにはどうすればよいですか?

{ 
    "header1" : { 
     "message" : { 
      "content" : "Hello", 
      "type" : "text" 
     }, 
     "header2" : { 
      "address" : "[email protected]" 
     } 
    } 
} 

でサーバーに送信したいと思い、このJSONファイルを持っています。

  jsonObject.put("content", "hello"); 
      jsonObject.put("type", "text"); 
      jsonObject.put("address", "[email protected]"); 
      String message = jsonObject.toString(); 

私の質問はどのように階層をコード化するのですか?header1、message、およびheader2?

答えて

0

私はボトムアップファッションでこれを行うべきだと思います。

私が意味するのは、最初に名前がheader2のJSONObjectを作成し、それにaddressを入れます。

messageという名前の他のJSONObjectを作成し、putを使用してポピュレートし、header1という名前の別のJSONObjectの中に配置します。その後

JSONObject header2 = new JSONObject(); 
header2.put("address", "your address"); 

JSONObject header1 = new JSONObject(); 
header1.put("header2", header2); 

のように...

1
JSONObject json = new JSONObject(); 
    JSONObject messageObject = new JSONObject(); 
    JSONObject header2Object = new JSONObject(); 
    try { 

     messageObject .put("content", "Hello"); 
     messageObject .put("type", "text"); 

     header2Object .put("address", "[email protected]"); 

     json.put("header1", messageObject.tostring); 
     json.put("header2", header2Object.tostring); 
    } catch (Exception ignored) { 
    } 

この

をお試しください
関連する問題