最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to create a nested json in java - Stack Overflow

programmeradmin2浏览0评论

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",

                }
                   ]
             ]
       ]
     ]
}
}   

and here is the java code.

    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();

The Problem:

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) is alerting an object and 2nd alert alert(json) is not showing anything.

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",

                }
                   ]
             ]
       ]
     ]
}
}   

and here is the java code.

    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();

The Problem:

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) is alerting an object and 2nd alert alert(json) is not showing anything.

Share Improve this question edited May 14, 2012 at 6:51 JN_newbie asked May 14, 2012 at 5:57 JN_newbieJN_newbie 6,11216 gold badges69 silver badges105 bronze badges 5
  • 1 And what is the problem? Be more specific please. – Aleksandr Kravets Commented May 14, 2012 at 6:02
  • 1 That's not valid JSON. (Your innermost "objects" mix the curly brackets of object literal syntax with the ma-separated value syntax of array literals. Some of the other properties do the opposite, i.e., mix the square brackets of array literal syntax with the property:value syntax of object literals.) – nnnnnn Commented May 14, 2012 at 6:08
  • Aleksandr I have edit the question. – JN_newbie Commented May 14, 2012 at 6:16
  • The edit didn't pletely fix the invalid JSON issue: you've fixed the innermost objects, but not the arrays (the ones beginning on the third and fourth lines of the would-be JSON). – nnnnnn Commented May 14, 2012 at 6:38
  • It's still wrong. As per my previous ment, you can't have name:value pairs inside square brackets, so the arrays beginning on the third and fourth lines of your would-be JSON are invalid. – nnnnnn Commented May 14, 2012 at 7:05
Add a ment  | 

2 Answers 2

Reset to default 2

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 [].

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)

  1. 暂无评论