当前位置:网站首页>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
边栏推荐
- Is Huishang futures platform safe? Is it OK to open a futures account?
- Azure key vault (1) Introduction
- You can't just focus on flex layout and elaborate animation to explain all flex layout methods! Easy to understand dry goods tutorial
- What is the ranking of good securities companies? Is online account opening safe
- R language ggplot2 visualization: ggplot2 visualization basic scatter plot, through in theme_ Specify the parameter base in BW_ Size to change the size of axis labels and control the size of gridlines
- C - partial keyword
- How to choose the appropriate data type for fields in MySQL?
- Varnish4.0 cache agent configuration
- Mysql8 encountered the problem of stopping after the service was started
- Dynamics 365: how to get the authentication information required to connect to D365 online from azure
猜你喜欢
![[adaptiveavgpool3d] pytorch tutorial](/img/d0/60ee74ff554effa06084d5d01a03e1.png)
[adaptiveavgpool3d] pytorch tutorial

Mlx90640 infrared thermal imager temperature measurement module development notes (III)

Arduino IDE ESP32固件安装和升级教程

iptables常用命令小清单

Withdrawal of IPO application, Yunzhou intelligent "tour" of unmanned boat enterprise fails to enter the science and Technology Innovation Board

Will the capital market be optimistic about TCL's folding screen story?

未来数据库需要关心的硬核创新

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

MySQL之知识点(十二)

城市安全系列科普丨进入溺水高发期,救生常识话你知
随机推荐
If this.$router Push the same address with different parameters, and the page does not refresh
【SWT】自定义数据表格
How to deal with being attacked? Advanced anti DDoS IP protection strategy
Dynamics 365: how to get the threshold value of executemullerequest in batch requests
【LOJ3247】「USACO 2020.1 Platinum」Non-Decreasing Subsequences(DP,分治)
有了这个机器学习画图神器,论文、博客都可以事半功倍了!
Memorythrashing: Tiktok live broadcast to solve memory dithering practice
[acwing] 909. Chess game
【洛谷】P1908 逆序对
[SWT] scrolling container to realize commodity list style
收益率在百分之六以上的理财产品,请说一下
Do you understand the working principle of gyroscope?
自适应设计和响应式设计
Kubernetes GPU's Dilemma and failure
R语言可视化分面图、多变量分组嵌套多水平t检验、并指定参考水平、可视化多变量分组嵌套多水平分面箱图(faceting boxplot)并添加显著性水平、指定显著性参考水平
从哪些维度评判代码质量的好坏?如何具备写出高质量代码的能力?
Dynamics crm: how to set the order of forms
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
31 next spread
2.19 haas506 2.0开发教程 - bluetooth - 蓝牙通信(仅支持2.2以上版本)