借助Lambda表达式将json字符串转成 Map<String ,List<Object>>类型

   今天上班时遇到了这个问题,顺便记一下,废话不多说,直接上代码

1.自己造的一个实体类如下:

package com.nrsc.json_to_map;

public class Dog {
	
	private String name;
	private int age;
	private String color;

	public Dog(String name, int age, String color) {
		super();
		this.name = name;
		this.age = age;
		this.color = color;
	}

	public String getName() {return name;}
	public void setName(String name) {this.name = name;}

	public int getAge() {return age;}
	public void setAge(int age) {this.age = age;}

	public String getColor() {return color;}
	public void setColor(String color) {this.color = color;}

	@Override
	public String toString() {
		return "Dog [name=" + name + ", age=" + age + ", color=" + color + "]";
	}
}

2.解决方式如下:

package com.nrsc.json_to_map;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;
public class Demo {
    public static void main(String[] args) {
        /** 小明狗的集合 */
        Dog[] XM_DOG = {new Dog("大黄", 2, "黄色"),
                new Dog("小黑", 3, "黑色")};
        List<Dog> XM_DOG_List = Arrays.asList(XM_DOG);

        /** 小亮狗的集合 */
        Dog[] XL_DOG = {new Dog("二狗", 6, "黑白色"),
                new Dog("卷毛", 5, "白色")};
        List<Dog> XL_DOG_List = Arrays.asList(XL_DOG);

        Map<String, List<Dog>> map = new HashMap<>();
        map.put("xiaoming", XM_DOG_List);
        map.put("xiaoliang", XL_DOG_List);
        // 利用FastJson先将map转为json字符串
        String jsonString = JSONObject.toJSONString(map);

        System.out.println(jsonString);

        System.out.println("=================================================================");

        /** 获取到JSONObject */
        JSONObject parseObject = JSONObject.parseObject(jsonString);

        /** parseObject.entrySet()获取到Set集合 */
        Set<Entry<String, Object>> entrySet = parseObject.entrySet();

        /** set集合里都有什么 */
        for (Entry<String, Object> entry : entrySet) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }

        System.out.println("=================================================================");
        /**
         * 熟悉了JSONObject以及parseObject.entrySet()之后
         * 就可以比较容易地明白怎么通过lambda表达式将整个json字符串转换成Map>了
         *
         * 表达式如下:
         */

        Map<String, List<Dog>> result = JSONObject.parseObject(jsonString).entrySet()
                .stream().collect(Collectors.toMap(Map.Entry::getKey,
                        entry -> JSONObject.parseArray(String.valueOf(entry.getValue()), Dog.class)));

        List<Dog> list = result.get("xiaoming");
        List<Dog> list2 = result.get("xiaoliang");

        System.out.println(list);
        System.out.println(list2);
    }
}

贴一下运行结果

借助Lambda表达式将json字符串转成 Map<String ,List<Object>>类型_第1张图片

你可能感兴趣的:(Lambda表达式,Json转Map<String,List<Object>>,lambda表达式,FastJson)