0
点赞
收藏
分享

微信扫一扫

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

念川LNSC 2022-03-25 阅读 233
javajson

复杂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 条评论