如何使用JSONObject构建JSON时嵌套对象 [英] How to nest objects when building JSON with JSONObject

查看:1284
本文介绍了如何使用JSONObject构建JSON时嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此字符串编码为 POST 请求。任何人都可以告诉我如何编码

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", "params":{"media":"music"}, "id": 1}

到目前为止,我已经

JSONOjbect obj = new JSONObject();
obj.put("jsonrpc", "2.0");
obj.put("method", "Files.GetSources");

但我不知道如何处理剩下的事情 - 有人可以帮忙吗?

But I'm not sure how to put in the rest - can anyone help?

推荐答案

如果你问如何将嵌套的 params 对象放在那里你可能会这样做:

If you're asking how you'd put the nested params object in there, you'd probably do:

JSONObject params = new JSONObject();
params.put("media", "music");

obj.put("params", params);






使用数组(根据下面的评论) ),你会做这样的事情:


To use an array (per your comments below), you'd do something like this:

JSONArray properties = new JSONArray();
properties.put("resume");
properties.put("genre");
properties.put("studio");
...

JSONObject params = new JSONObject();
params.put("properties", properties);

obj.put("params", params);

这篇关于如何使用JSONObject构建JSON时嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆