How to nest objects when building JSON with JSONObject

前端 未结 4 742
你的背包
你的背包 2021-02-07 20:52

I\'m trying to encode this string for a POST request. Can anyone tell me how I can encode

{\"jsonrpc\": \"2.0\", \"method\": \"Files.GetSources\",         


        
4条回答
  •  忘掉有多难
    2021-02-07 21:21

    JSONOjbect obj = new JSONObject().put("jsonrpc", "2.0")
        .put("method", "Files.GetSources").put("id", 1)
        .put("params", new JSONObject().put("media", "music"));
    

    Chaining .put() like this is possible because put() returns the object it was called on - for this exact purpose.

提交回复
热议问题