JSONArray合并
时间: 2023-12-16 19:03:21 浏览: 107
要合并两个JSONArray,可以使用以下代码:
```java
public static String joinJSONArray(JSONArray mData, JSONArray array) {
StringBuffer buffer = new StringBuffer();
buffer.append("[");
try {
int len = mData.size();
for (int i = 0; i < len; i++) {
JSONObject obj1 = (JSONObject) mData.get(i);
if (i == len - 1) {
buffer.append(obj1.toString());
} else {
buffer.append(obj1.toString()).append(",");
}
}
len = array.size();
if (len > 0) {
buffer.append(",");
}
for (int i = 0; i < len; i++) {
JSONObject obj1 = (JSONObject) array.get(i);
if (i == len - 1) {
buffer.append(obj1.toString());
} else {
buffer.append(obj1.toString()).append(",");
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("合并jsonarray异常", e);
}
buffer.append("]");
return buffer.toString();
}
```
阅读全文