0

我在用 java 制作 json 时遇到问题。下面是我必须通过 java 代码创建的 JSON。

{"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 时。输出未显示。

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

alert(data) 正在提醒一个对象,第二个警报 alert(json) 没有显示任何内容。

4

2 回答 2

2

您的 JSON 对象语法错误:对象必须包含字段/值对列表并用大括号 {} 括起来,数组是用尖括号 [] 括起来的值列表。

于 2012-05-14T06:10:37.503 回答
2

示例 json 错误,请更新

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

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

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

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

    {
        "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 代码

        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();
        }
于 2014-08-19T10:04:02.740 回答