如何在Java中创建嵌套的json [英] how to create a nested json in java

查看:226
本文介绍了如何在Java中创建嵌套的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用Java创建json时遇到问题.下面是我必须通过Java代码创建的JSON.

I am having a problem in making a json in java. below is the JSON which I have to create through java code.

{"status":"0",
"Response":{ 
    "abc":[
        "def":[
            "fgh":[
                {
                    "abc":"abc",
                    "def":"abc",
                    "ghi":"abc",

                },
                {
                    "abc":"abc",
                    "def":"abc",
                    "ghi":"abc",

                },
                {
                    "abc":"abc",
                    "def":"abc",
                    "ghi":"abc",

                }
                  ],
            "ghi":[
                {
                    "abc":"abc",
                    "def":"abc",
                    "ghi":"abc",

                },
                {
                    "abc":"abc",
                    "def":"abc",
                    "ghi":"abc",

                },
                {
                    "abc":"abc",
                    "def":"abc",
                    "ghi":"abc",

                }
                   ]
             ]
       ]
     ]
}
}   

这是Java代码.

    JSONObject result = new JSONObject();
    JSONObject abcObject = new JSONObject();
    JSONArray resultArray = new JSONArray();
    JSONArray fghArray = new JSONArray();
    JSONArray defArray = new JSONArray();
    JSONArray abcArray = new JSONArray();

abcObject.put("abc");
abcObject.put("def");
abcObject.put("ghi");
fghArray.add(abcObject);
defArray.add(fghArray);
abcArray.add(defArray);
result.put("status", 0);
result.put("Response",abcArray); 
return resultJson.toString();

问题:

当我将json发送回jsp时.输出未显示.

when i send back the json to a jsp. the output is not showing up.

success:function(data) {
            alert(data);
           var json = $.toJSON(data); 
           alert(json);
        },

alert(data)正在警告对象,而第二个alert alert(json)没有显示任何内容.

alert(data) is alerting an object and 2nd alert alert(json) is not showing anything.

推荐答案

示例json错误,请更新

The Example json is Wrong Please Update it

JSON对象应该像

    {
        "key1":"value",
        "key2":"value2"
    }

JSON数组

    [
        {
            "key1":"value",
            "key2":"value2"
        },
        {
            "key1":"value",
            "key2":"value2"
        }
        ]

你不能拥有

    //Wrong Array format (Array should be list of Objects)
    [
        "key1": "value",
        "key2": "value2"
    ]

当您嵌套它们时,它们应该像

and when you nest them they should be like

    {"key1": "value1",
        "key2": [
            {
                "key1": "value",
                "key2": "value2"
            },
            {
                "key1": "value",
                "key2": "value2"
            }
        ],
        "key3": [
            {
                "key1": "value",
                "key2": "value2"
            },
            {
                "key1": "value",
                "key2": "value2"
            }
        ]
    }

由于您在上面的示例中使用了一对,因此我已将上述JSON数组更改为JSON对象 新的示例JSON是

Since you where using a pair in your above example i have changed the above JSON array to a JSON Object the new sample JSON is

    {
        "status": "0",
        "Response": {
            "abc": {
                "def": {
                    "fgh": [
                        {
                            "abc": "abc",
                            "def": "abc",
                            "ghi": "abc"
                        },
                        {
                            "abc": "abc",
                            "def": "abc",
                            "ghi": "abc"
                        },
                        {
                            "abc": "abc",
                            "def": "abc",
                            "ghi": "abc"
                        }
                    ],
                    "ghi": [
                        {
                            "abc": "abc",
                            "def": "abc",
                            "ghi": "abc"
                        },
                        {
                            "abc": "abc",
                            "def": "abc",
                            "ghi": "abc"
                        },
                        {
                            "abc": "abc",
                            "def": "abc",
                            "ghi": "abc"
                        }
                    ]
                }
            }
        }
    }

以及用于创建上述JSON对象的Java代码

And the Java Code to Create the Above JSON Object

        import org.codehaus.jettison.json.JSONArray;
        import org.codehaus.jettison.json.JSONException;
        import org.codehaus.jettison.json.JSONObject;
        public static String createJson() {
            JSONObject result = new JSONObject();
            JSONObject response = new JSONObject();
            JSONObject abc = new JSONObject();
            JSONObject def = new JSONObject();
            JSONArray fgh = new JSONArray();
            JSONArray ghi = new JSONArray();
            JSONObject sampleInnerElement = new JSONObject();
            try {
                sampleInnerElement.put("abc","abc");
                sampleInnerElement.put("def","abc");
                sampleInnerElement.put("ghi","abc");
                //populating the fgh Array
                fgh.put(sampleInnerElement);
                fgh.put(sampleInnerElement);
                fgh.put(sampleInnerElement);
                //populating the Ghi Array
                ghi.put(sampleInnerElement);
                ghi.put(sampleInnerElement);
                ghi.put(sampleInnerElement);

                def.put("fgh",fgh);
                def.put("ghi",ghi);
                abc.put("def",def);
                response.put("abc",abc);
                result.put("status","0");
                result.put("response",response);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return result.toString();
        }

这篇关于如何在Java中创建嵌套的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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