创建嵌套JSON对象使用的JSONObject Java中的以下结构? [英] Creating nested JSON object for the following structure in Java using JSONObject?

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

问题描述

我想建立一个JSON对象类似以下使用的JSONObject和JSONArray Java中的结构。

我已经通过堆栈溢出,这表明使用类似的方法推动各个岗位消失了,把等,这我不能确定JSONArray。请帮忙。

  {
    名:样品,
    DEF:
        {
            SETID:1,
            SETDEF:[
                {
                    名:ABC,
                    类型:STRING
                },
                {
                    名:XYZ,
                    类型:STRING
                }
            ]
        },
        {
            SETID:2,
            SETDEF:[
                {
                    名:ABC,
                    类型:STRING
                },
                {
                    名:XYZ,
                    类型:STRING
                }
            ]
        }
    ]
}


解决方案

随着进口 org.json.JSONArray org.json.JSONObject

 的JSONObject对象=新的JSONObject();
object.put(名,样品);
JSONArray阵列=新JSONArray();JSONObject的arrayElementOne =新的JSONObject();
arrayElementOne.put(SETID,1);
JSONArray arrayElementOneArray =新JSONArray();JSONObject的arrayElementOneArrayElementOne =新的JSONObject();
arrayElementOneArrayElementOne.put(姓名,ABC);
arrayElementOneArrayElementOne.put(类型,STRING);JSONObject的arrayElementOneArrayElementTwo =新的JSONObject();
arrayElementOneArrayElementTwo.put(姓名,XYZ);
arrayElementOneArrayElementTwo.put(类型,STRING);arrayElementOneArray.put(arrayElementOneArrayElementOne);
arrayElementOneArray.put(arrayElementOneArrayElementTwo);arrayElementOne.put(SETDEF,arrayElementOneArray);
array.put(arrayElementOne);
object.put(DEF,数组);

我不包括第一阵列为清楚起见,第二个元素。希望你有一点,但。

编辑:

在previous答案是假设你使用 org.json.JSONObject org.json.JSONArray

有关 net.sf.json.JSONObject net.sf.json.JSONArray

 的JSONObject对象=新的JSONObject();
object.element(名,样品);
JSONArray阵列=新JSONArray();JSONObject的arrayElementOne =新的JSONObject();
arrayElementOne.element(SETID,1);
JSONArray arrayElementOneArray =新JSONArray();JSONObject的arrayElementOneArrayElementOne =新的JSONObject();
arrayElementOneArrayElementOne.element(姓名,ABC);
arrayElementOneArrayElementOne.element(类型,STRING);JSONObject的arrayElementOneArrayElementTwo =新的JSONObject();
arrayElementOneArrayElementTwo.element(姓名,XYZ);
arrayElementOneArrayElementTwo.element(类型,STRING);arrayElementOneArray.add(arrayElementOneArrayElementOne);
arrayElementOneArray.add(arrayElementOneArrayElementTwo);arrayElementOne.element(SETDEF,arrayElementOneArray);
object.element(DEF,数组);

基本上是一样的,取代法'把'为'放'在JSONArray加在JSONObject的元素,和

I want to build a JSON Object similar to following the structure in java using JSONObject and JSONArray.

I have gone through various posts in stack overflow, which suggests using methods like push, put etc which I am unable to identify for JSONArray. Please help.

{
    "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:

The previous answer was assuming you were using org.json.JSONObject and org.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对象使用的JSONObject Java中的以下结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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