How to nest objects when building JSON with JSONObject

前端 未结 4 1996
梦如初夏
梦如初夏 2021-02-07 20:34

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:36

    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.

提交回复
热议问题