81,094
社区成员
发帖
与我相关
我的任务
分享
{
"list1": [
{
"id": "238",
"username": "凤20",
"peiouid": "255,258"
}
],
"list2": [
{
"id": "255",
"username": "wo1",
"peiouid": null
},
{
"id": "258",
"username": "1231132",
"peiouid": ""
}
]
}
{
"list1": [
{
"id": "238",
"username": "凤20",
"peiouid": "255,258",
"list2": [
{
"id": "255",
"username": "wo1",
"peiouid": null
},
{
"id": "258",
"username": "1231132",
"peiouid": ""
}
]
}
]
}
public class TestJson {
public static void main(String[] args) {
String s = "{" + "\"list1\": [" + "{" + "\"id\": \"238\","
+ "\"username\": \"凤20\"," + "\"peiouid\": \"255,258\"" + "}"
+ "]," + "\"list2\": [" + "{" + "\"id\": \"255\","
+ "\"username\": \"wo1\"," + "\"peiouid\": null" + "}," + "{"
+ "\"id\": \"258\"," + "\"username\": \"1231132\","
+ "\"peiouid\": \"\"" + "}" + "]" + "}";
JSONObject jo = JSON.parseObject(s);
JSONArray joList1 = jo.getJSONArray("list1");
String peiouid = joList1.getJSONObject(0).getString("peiouid");
JSONArray jaList = new JSONArray();
JSONArray jaList2 = jo.getJSONArray("list2");
for(int i=0;i<jaList2.size();i++){
JSONObject ji = jaList2.getJSONObject(i);
if(peiouid.contains(ji.getString("id"))){
jaList.add(ji);
}
}
joList1.getJSONObject(0).put("list2", jaList);
JSONObject jsonObject = new JSONObject();
jsonObject.put("list1", joList1);
System.out.println(jsonObject);
}
}