合并、连接两个JSONArray方法
作者:互联网
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/*
add by xiaoyong
time 2020年3月30日
tag:合并jsonarray 这是jsonarray也是json格式才行 [{usersid:322,LevelID:'',permission:'1,2'},{usersid:324,LevelID:5,permission:'2,5,9'}]
*/
public static JSONArray joinJSONArray(JSONArray array1, JSONArray array2) {
StringBuffer sbf = new StringBuffer();
JSONArray jSONArray = new JSONArray();
try {
int len = array1.size();
for (int i = 0; i < len; i++) {
JSONObject obj1 = JSONObject.fromObject(array1.get(i));
if (i == len - 1)
sbf.append(obj1.toString());
else
sbf.append(obj1.toString()).append(",");
}
len = array2.size();
if (len > 0)
sbf.append(",");
for (int i = 0; i < len; i++) {
JSONObject obj2 = JSONObject.fromObject(array2.get(i));
if (i == len - 1)
sbf.append(obj2.toString());
else
sbf.append(obj2.toString()).append(",");
}
sbf.insert(0, "[").append("]");
jSONArray =JSONArray.fromObject(sbf.toString());
return jSONArray;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
标签:JSONArray,JSONObject,合并,len,sbf,toString,连接,append 来源: https://blog.csdn.net/sydadan/article/details/116303596