JSON对象转换为JAVA对象(key-value-children)

复杂json数据需要返回给前端作树形展示时,往往需要将数据封装成“key-value-children”的格式,为此,我写了一个转换工具方法,供大家参考。

构造一个测试JSON

{
    "status": 10000,
    "message": null,
    "data": {
        "id": "123",
        "name": "test1",
        "next": [
            {
                "id": "456",
                "name": "test2",
                "next": []
            },{
                "id": "789",
                "name": "test3",
                "next": []
            }
        ]
    },
    "timestamp": 1618826663594
}

json对象转换方法:

public static List<Object> convertJson2KvObj(JSONObject json){
        List<Object> objList = new ArrayList<>();
        for(Map.Entry<String, Object> entry : json.entrySet()){
            KeyValueObj obj = new KeyValueObj();
            obj.setKey(entry.getKey());
            if(entry.getValue() instanceof JSONObject){
                obj.setChildren(convertJson2KvObj((JSONObject)entry.getValue()));
            }else if(entry.getValue() instanceof JSONArray){
                List<Object> list = new ArrayList<>();
                for (Object o : (JSONArray) entry.getValue()) {
                    list.add(convertJson2KvObj((JSONObject) o));
                }
                obj.setChildren(list);
            }else{
                obj.setValue(entry.getValue() == null ? null : entry.getValue().toString());
            }
            objList.add(obj);
        }
        return objList;
    }

实体类

import lombok.Data;
import java.util.List;

@Data
public class KeyValueObj {

    private String key;
    private String value;
    private List<Object> children;
}

测试代码:

public static void main(String[] args) {
        String str = "{\n" +
                "    \"status\": 10000,\n" +
                "    \"message\": null,\n" +
                "    \"data\": {\n" +
                "        \"id\": \"123\",\n" +
                "        \"name\": \"test1\",\n" +
                "        \"next\": [\n" +
                "            {\n" +
                "                \"id\": \"456\",\n" +
                "                \"name\": \"test2\",\n" +
                "                \"next\": []\n" +
                "            },{\n" +
                "                \"id\": \"789\",\n" +
                "                \"name\": \"test3\",\n" +
                "                \"next\": []\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"timestamp\": 1618826663594\n" +
                "}";
        List<Object> objList = JsonUtil.convertJson2KvObj(JSON.parseObject(str));
        System.out.println(objList);
    }

结果打印(整理后):

[KeyValueObj(key=data, value=null, children=
	[KeyValueObj(key=next, value=null, children=
		[
			[KeyValueObj(key=next, value=null, children=null), 
			 KeyValueObj(key=name, value=test2, children=null), 
			 KeyValueObj(key=id, value=456, children=null)
			 ], 
			[KeyValueObj(key=next, value=null, children=null), 
			 KeyValueObj(key=name, value=test3, children=null), 
			 KeyValueObj(key=id, value=789, children=null)
			 ]
		]), 
	 KeyValueObj(key=name, value=test1, children=null), 
	 KeyValueObj(key=id, value=123, children=null)
	]), 
 KeyValueObj(key=message, value=null, children=null), 
 KeyValueObj(key=status, value=10000, children=null), 
 KeyValueObj(key=timestamp, value=1618826663594, children=null)
]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值