【发布时间】:2015-09-13 08:47:56
【问题描述】:
我正在尝试将 JSONObject 放入 Java 中的 JSONArray 中。这是我的两个对象:
JSON数组:
[{
"url": null,
"flag": "0",
"read": "0",
"time": 2000,
"exp": null,
"population": 10
}]
JSON 对象:
{
"events": [
{
"color": "Green",
"event": "Restart"
},
{
"color": "Black",
"event": "Shutdown"
},
{
"color": "White",
"event": "Read"
}
]
}
预期结果:
[
{
"url": null,
"flag": "0",
"read": "0",
"time": 2000,
"exp": null,
"population": 10,
"events": [
{
"color": "Green",
"event": "Restart"
},
{
"color": "Black",
"event": "Shutdown"
},
{
"color": "White",
"event": "Read"
}
]
}
]
我尝试使用这段代码,但结果不ok:
jsonArray.put(jsonObject);
意外的结果:
[
{
"url": null,
"flag": "0",
"read": "0",
"time": 2000,
"exp": null,
"population": 10
},
{
"events": [
{
"color": "Green",
"event": "Restart"
},
{
"color": "Black",
"event": "Shutdown"
},
{
"color": "White",
"event": "Read"
}
]
}
]
“事件”键值大部分位于 JSONArray 中的唯一元素内,而不是作为另一个元素。
【问题讨论】:
标签: java arrays json jsonobject