当前位置:网站首页>Traverse, delete, judge whether JSON data is empty, and recursion
Traverse, delete, judge whether JSON data is empty, and recursion
2022-07-24 15:58:00 【You are my forever bug】
newest fastjson jar package
JSONObject backDish = JSONObject.parseObject(val);
// obtain data data :"data":{"list":[{},{}]}
JSONObject da = backDish.getJSONObject("data");
// obtain list data [{},{}]
JSONArray list = da.getJSONArray("list");
JSONObject quanbu = new JSONObject();
quanbu.put("dishesType","");
quanbu.put("dishesTypeName"," All the dishes ");
quanbu.put("shopId","");
System.out.println("quanbu:"+quanbu.toString());
arrayDishType = new JSONArray();
//JSONArray Add JSONObject
arrayDishType.add(quanbu);
// Traverse JSONArray Array
for (int i = 0;i<list.size();i++){
System.out.println("list The content of "+(list.getJSONObject(i).toString()));
arrayDishType.add(list.getJSONObject(i));
}
System.out.println("22222"+arrayDishType.toString());
Back to json The data are as follows :
{
"msg": " According to successful ",
"code": 0,
"data": {
"list": [
{
"deptCreate": "2020/12/14-15:19:57",
"deptName": " jiangsu ",
"leader": "2",
"deptModified": "2020/12/14-15:20:33",
"son": "Y",
"children": [
{
"deptCreate": "2020/12/21-09:13:00",
"deptName": " Suzhou store ",
"leader": "2",
"son": "N",
"children": [],
"phone": "2",
"deptId": 15,
"orderNum": 0,
"email": "2",
"parentId": 9
},
{
"deptCreate": "2020/12/14-15:20:09",
"deptName": " Nanjing store ",
"leader": "3",
"deptModified": "2020/12/21-09:12:46",
"son": "N",
"children": [],
"phone": "3",
"deptId": 10,
"orderNum": 1,
"email": "3",
"parentId": 9
}
],
"phone": "2",
"deptId": 9,
"orderNum": 0,
"email": "2",
"parentId": 0
},
{
"deptCreate": "2020/12/14-15:20:42",
"deptName": " Zhejiang ",
"leader": "3",
"deptModified": "2020/12/14-15:21:43",
"son": "Y",
"children": [
{
"deptCreate": "2020/12/14-15:21:08",
"deptName": " Ningbo store ",
"leader": "23",
"son": "Y",
"children": [
{
"deptCreate": "2021-01-25 10:21:51",
"deptName": "wqqww",
"leader": "qw",
"son": "N",
"children": [],
"phone": "qwqw",
"deptId": 17,
"orderNum": 0,
"email": "w",
"parentId": 13
}
],
"phone": "31",
"deptId": 13,
"orderNum": 0,
"email": "12",
"parentId": 11
},
{
"deptCreate": "2020/12/14-15:20:54",
"deptName": " Hangzhou store ",
"leader": "4",
"deptModified": "2020/12/14-15:21:40",
"son": "N",
"children": [],
"phone": "34",
"deptId": 12,
"orderNum": 1,
"email": "43",
"parentId": 11
}
],
"phone": "3",
"deptId": 11,
"orderNum": 1,
"email": "3",
"parentId": 0
}
]
}
}
requirement : take children:[ ] To remove
- take data Corresponding value
JSONObject baseInfo = jsonSuccess.getJSONObject("data");
System.out.println("base:"+baseInfo);
result :{“list”:[{},{},{}…]}
2、 take list Corresponding value
JSONArray value = null;
for (Map.Entry<String, Object> entry : baseInfo.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());//list : [{"children":[{"children":[],"name":"hh"}],"name":"ls"}]
value = (JSONArray) entry.getValue();// [{"children":[{"children":[],"name":"hh"}],"name":"ls"}]
}
result : [{},{}…]
Call recursive traversal method
recursive.json(value);
3、 Traverse jsonArray Array 、 Delete json Key value pair , Judge whether it is 【】 And recursion
public class recursive {
public static void json(JSONArray value32) {
JSONArray value3 = null;
// An array 【】 Traverse
for (int i = 0; i < ((JSONArray) value32).size(); i++) {
// Get array's data
JSONObject house = (JSONObject) ((JSONArray) value32).get(i);
// Traverse the key value pairs of the array
for (Map.Entry<String, Object> entry : house.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
// If key ==children
if(entry.getKey().equals("children")) {
// take children The value of value3
value3 = (JSONArray) entry.getValue(); //[{"children":[],"name":"hh"}]
}
}
// Judge children Value Is it right? 【】
if(("[]".equals(value3.toString()))) {
System.out.println(" It's empty ");
// Delete children Key value pair Delete... Here json The original data Also delete
house.remove("children");
}else {
// If it's not empty , explain children Still worth it , recursive Call yourself , Traverse the next layer again children
json(value3);
}
}
}
Straw paper
package menu_crud;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class rt {
public static void main(String[] args) {
String originalStr = "{'data':{'list':[{'name':'ls','children':[{'name':'hh','children':[]}]}]}}";
JSONObject jsonObject = JSON.parseObject(originalStr);
System.out.println(jsonObject);
JSONObject baseInfo = jsonObject.getJSONObject("data");
System.out.println("base:"+baseInfo); //{"list":[{"children":[{"children":[],"name":"hh"}],"name":"ls"}]}
Object value = null;
for (Map.Entry<String, Object> entry : baseInfo.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());//list : [{"children":[{"children":[],"name":"hh"}],"name":"ls"}]
value = entry.getValue();// [{"children":[{"children":[],"name":"hh"}],"name":"ls"}]
}
System.out.println("value:"+value);
if(value instanceof JSONArray) {
System.out.println("11111");
}
// An array 【】 Traverse
Object value2 = null;
for (int i = 0; i < ((JSONArray) value).size(); i++) {
JSONObject house = (JSONObject) ((JSONArray) value).get(i);
for (Map.Entry<String, Object> entry : house.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
if(entry.getKey().equals("children")) {
value2 = entry.getValue(); //[{"children":[],"name":"hh"}]
}
}
}
System.out.println("value2:"+value2);
Object value3 = null;
// An array 【】 Traverse
for (int i = 0; i < ((JSONArray) value2).size(); i++) {
JSONObject house = (JSONObject) ((JSONArray) value2).get(i);
for (Map.Entry<String, Object> entry : house.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
if(entry.getKey().equals("children")) {
value3 = entry.getValue(); //[{"children":[],"name":"hh"}]
}
}
if(("[]".equals(value3.toString()))) {
System.out.println(" It's empty ");
house.remove("children");
}
}
System.out.println("value3:"+value3);
System.out.println("jsonObject:"+jsonObject);
}
}
Reference resources :https://blog.csdn.net/AttleeTao/article/details/104414739?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522161162527916780264086665%252522%25252C%252522scm%252522%25253A%25252220140713.130102334…%252522%25257D&request_id=161162527916780264086665&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-2-104414739.pc_search_result_hbase_insert&utm_term=%E6%80%8E%E4%B9%88%E9%81%8D%E5%8E%86%E5%B5%8C%E5%A5%97%E7%9A%84JSONObject
边栏推荐
- Leetcode 220. 存在重复元素 III
- [adaptiveavgpool3d] pytorch tutorial
- Please talk about the financial products with a yield of more than 6%
- Dynamics crm: how to set the order of forms
- Varnish4.0缓存代理配置
- Arduino IDE ESP32固件安装和升级教程
- Array in PHP_ The pit of merge
- 请问好的券商的排名?网上开户安全吗
- Configuring WAPI certificate security policy for Huawei wireless devices
- Programming in CoDeSys to realize serial communication [based on raspberry pie 4B]
猜你喜欢

Yolov3 trains its own data set

Dynamics crm: how to set the order of forms

【LOJ3247】「USACO 2020.1 Platinum」Non-Decreasing Subsequences(DP,分治)

How to deal with being attacked? Advanced anti DDoS IP protection strategy

Using JS to implement click events

【SWT】滚动容器实现商品列表样式

Arduino ide esp32 firmware installation and upgrade tutorial

Some understanding of the rank sum of matrix and the rank of image

yolov4 训练自己的数据集

There are more than 20 categories of the four operators in MySQL. It took three days and three nights to sort them out. Don't collect them quickly
随机推荐
【LeetCode】Day102-螺旋矩阵 II
Using JS to implement click events
IP protocol - network segment division
3、 Set foundation ArrayList set and simple student management system
Leetcode 231. 2 的幂
yolov4 训练自己的数据集
If this.$router Push the same address with different parameters, and the page does not refresh
MySQL之知识点(十二)
Dynamics crm: how to set the order of forms
自适应设计和响应式设计
Deploy ZABBIX monitoring system and email alarm mechanism in lamp architecture
Who is the "roll" king of the prefabricated vegetable track?
Dynamics 365: how to get the threshold value of executemullerequest in batch requests
Do you understand the working principle of gyroscope?
Simplified understanding: publish and subscribe
Research on the efficiency of numpy array access
[adaptiveavgpool3d] pytorch tutorial
Choice of advanced anti DDoS IP and CDN in case of DDoS
Exomeiser annotates and prioritizes exome variants
torch_ How to use scatter. Scatter() in detail