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

我已经阅读了stack overflow中的各种帖子,其中建议使用push、put等我无法识别的JSONArray方法.请帮忙.

{
    "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"
                }
            ]
        }
    ]
}

推荐答案

With the imports org.json.JSONArray and org.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);

I did not include first array's second element for clarity. Hope you got the point though.

EDIT:

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

For net.sf.json.JSONObject and net.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);

Basically it's the same, replacing the method 'put' for 'element' in JSONObject, and 'put' for 'add' in JSONArray.

Json相关问答推荐

使用jolt删除空对象

您可以使用Jolt对JSON执行OR条件吗

Vega Lite中的图例对齐

在Golang中从 struct 手动创建JSON对象

在Ruby的json中压缩单个字段

Jolt - 如何比较 if else 条件的两个值

如何使用 JOLT 将带有列表的 JSON 项目取消列出为多个项目?

JOLT JSON 将值从一对多转换为一对一

如何在 Android Studio 中将 List 字段作为空列表[]返回?

坚持弄清楚如何使用 api 响应来调用以从不同的链接检索响应

自定义将 struct 解组为切片映射

无法向 Json 数组添加新元素

如何使用 gson 将数据保存在 json 文件中?

规范化 JSON 文件

在 Django 1.9 中,使用 JSONField(本机 postgres jsonb)的约定是什么?

有没有办法使用 Jackson 将 Map 转换为 JSON 表示而不写入文件?

使用 jq 如何用其他名称替换键的名称

如何在 Java 中将 YAML 转换为 JSON?

Python 到 JSON 序列化在十进制上失败

Volley JsonObjectRequest Post 参数不再起作用