how to create a nested json in java

前端 未结 2 1797
滥情空心
滥情空心 2021-01-28 13:00

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\":[
            


        
相关标签:
2条回答
  • 2021-01-28 13:39

    The Example json is Wrong Please Update it

    A JSON object should be like

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

    A JSON Array

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

    you cannot have

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

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

    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();
            }
    
    0 讨论(0)
  • 2021-01-28 13:57

    Your JSON object has a wrong syntax: An object must contain a list of field/value pairs and is enclosed with braces {}, an array is a list of values enclosed with angle brackets [].

    0 讨论(0)
提交回复
热议问题