【发布时间】:2016-08-27 03:31:21
【问题描述】:
您好,我在 JSONObject 中合并 JSONArray 时遇到问题。下面是我的 JSONObject 的样子:
{
"name":"sample.bin.png",
"coords":{
"1":{"x":[ 974, 975],"y":[154, 155},
"3":{"x":[124, 125],"y":[529]},
"8":{"x":[2048, 2049],"y":[548, 560, 561, 562, 563, 564 ]}
}
}
现在我有了想要合并的那些 JSONObject 的键(在 coords 内)。我想将 x 和 y 分别合并到一个 JSONObject 中,这是我的代码:
String[] tokens = request().body().asFormUrlEncoded().get("coords")[0].split(","); //here i recieve the String Array Keys of the coords i want to merge
if (!image.equals("")) {
JSONObject outputJSON = getImageJSON(image); //here comes the JSON which i posted above
JSONObject coordsPack = (JSONObject) outputJSON.get("coords");
JSONObject merged = new JSONObject();
merged.put("x", new JSONArray());
merged.put("y", new JSONArray());
for (String index : tokens) {
JSONObject coordXY = (JSONObject) coordsPack.get(index);
JSONArray xList = (JSONArray) coordXY.get("x");
JSONArray yList = (JSONArray) coordXY.get("y");
merged.get("x").addAll(xList);
merged.get("y").addAll(yList);
}
System.out.println(merged);
}
但问题是我在merged.get("x").addAll(xList); 和merged.get("y").addAll(yList); 遇到错误,我无法访问这些方法。
【问题讨论】:
标签: java json merge array-merge json-simple