java - 使用 JSONObject 在 Java 中为以下结构创建嵌套 JSON 对象?

我想使用 JSONObject 和 JSONArray 构建一个类似于 java 中的结构的 JSON 对象。

我浏览了堆栈溢出中的各种帖子,其中建议使用我无法识别 JSONArray 的 push、put 等方法。请帮忙。

{
    "name": "sample",
    "def": [
        {
            "setId": 1,
            "setDef": [
                {
                    "name": "ABC",
                    "type": "STRING"
                },
                {
                    "name": "XYZ",
                    "type": "STRING"
                }
            ]
        },
        {
            "setId": 2,
            "setDef": [
                {
                    "name": "abc",
                    "type": "STRING"
                },
                {
                    "name": "xyz",
                    "type": "STRING"
                }
            ]
        }
    ]
}

最佳答案

使用导入 org.json.JSONArrayorg.json.JSONObject

JSONObject object = new JSONObject();
object.put("name", "sample");
JSONArray array = new JSONArray();

JSONObject arrayElementOne = new JSONObject();
arrayElementOne.put("setId", 1);
JSONArray arrayElementOneArray = new JSONArray();

JSONObject arrayElementOneArrayElementOne = new JSONObject();
arrayElementOneArrayElementOne.put("name", "ABC");
arrayElementOneArrayElementOne.put("type", "STRING");

JSONObject arrayElementOneArrayElementTwo = new JSONObject();
arrayElementOneArrayElementTwo.put("name", "XYZ");
arrayElementOneArrayElementTwo.put("type", "STRING");

arrayElementOneArray.put(arrayElementOneArrayElementOne);
arrayElementOneArray.put(arrayElementOneArrayElementTwo);

arrayElementOne.put("setDef", arrayElementOneArray);
array.put(arrayElementOne);
object.put("def", array);

为了清楚起见,我没有包含第一个数组的第二个元素。不过希望你明白了。

编辑:

之前的答案是假设您使用的是 org.json.JSONObjectorg.json.JSONArray

对于 net.sf.json.JSONObjectnet.sf.json.JSONArray :

JSONObject object = new JSONObject();
object.element("name", "sample");
JSONArray array = new JSONArray();

JSONObject arrayElementOne = new JSONObject();
arrayElementOne.element("setId", 1);
JSONArray arrayElementOneArray = new JSONArray();

JSONObject arrayElementOneArrayElementOne = new JSONObject();
arrayElementOneArrayElementOne.element("name", "ABC");
arrayElementOneArrayElementOne.element("type", "STRING");

JSONObject arrayElementOneArrayElementTwo = new JSONObject();
arrayElementOneArrayElementTwo.element("name", "XYZ");
arrayElementOneArrayElementTwo.element("type", "STRING");

arrayElementOneArray.add(arrayElementOneArrayElementOne);
arrayElementOneArray.add(arrayElementOneArrayElementTwo);

arrayElementOne.element("setDef", arrayElementOneArray);
object.element("def", array);

基本上是一样的,在 JSONObject 中将 'put' 方法替换为 'element',在 JSONArray 中将 'put' 方法替换为 'add'。

https://stackoverflow.com/questions/22042638/

相关文章:

javascript - 将 JSON 数据从 php 传递给 html-data 属性,然后传递给

jquery - 获取带有 AJAX 错误的服务器响应?

android - 如何调试我的改造 API 调用?

c# - 发布到 Web API 时出现不支持的媒体类型错误

c# - 模型绑定(bind)新的 Datatables 1.10 参数

jquery - 来源http ://localhost is not allowed by Acc

ruby-on-rails - rails jbuilder - 只是一个字符串数组

java - 没有很多类的 GSON 解析

java - 如何使用 Jackson 注释从 HttpResponse 反序列化 JSON 对象?

ajax - django:使用 json 对象测试基于 POST 的 View