【问题标题】:how to create a nested json in java如何在java中创建嵌套的json
【发布时间】:2012-05-21 15:04:37
【问题描述】:

我在用 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) 没有显示任何内容。

【问题讨论】:

  • 还有什么问题?请说得更具体些。
  • 这不是有效的 JSON。 (您最里面的“对象”将对象文字语法的大括号与数组文字的逗号分隔值语法混合在一起。其他一些属性则相反,即将数组文字语法的方括号与property:value 语法混合对象字面量。)
  • Aleksandr 我已经编辑了问题。
  • 编辑并没有完全解决无效的 JSON 问题:您已经修复了最里面的对象,但没有修复数组(从可能的 JSON 的第三和第四行开始的数组)。
  • 还是错了。根据我之前的评论,您不能在方括号内使用 name:value 对,因此从您的可能 JSON 的第三行和第四行开始的数组是无效的。

标签: java javascript json


【解决方案1】:

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

【讨论】:

    【解决方案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();
            }
    

    【讨论】:

      猜你喜欢
      • 2023-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多