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

}

]

]

]

]

}

}

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.

解决方案

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值