java 使用fast json 与 Map List互相转换

import com.alibaba.fastjson.JSON;
import com.imooc.vo.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JsonMapListUtil {
    public static void main(String[] args) {

        // Map 与 Json 互转
        Map<Integer,Integer> integerMap=Map.of(11,22,44,55,66,77,100,200);
        String integerMapString= JSON.toJSONString(integerMap,true);
        System.out.println("integerMapString:"+integerMapString);

        //默认类型 会转换成JSONObject类型,即 Map<String,Object>的子类
        Map<String,Object> objMap=JSON.parseObject(integerMapString);
        System.out.println("objMap: "+objMap);
        //objMap: {"66":77,"11":22,"44":55,"100":200} 此时的key类型为String,与之前不一致

        // 后面加 Map.class 转换为 Map类型,这个方式不是太好
        Map<Integer,Integer> objMap22=JSON.parseObject(integerMapString,Map.class);
//        Map<Object,Object> objMap22=JSON.parseObject(integerMapString,Map.class);
        System.out.println("objMap22: "+objMap22);
        // {66=77, 100=200, 11=22, 44=55}  此时结果是正确的,与之前的类型一致
        
        for (Map.Entry<Integer,Integer> entry:objMap22.entrySet()){
            System.out.println(entry.getKey() instanceof Integer); //全部 true
            System.out.println(entry.getValue() instanceof Integer);//全部 true
        }

     
Map<String,Integer> obj = new HashMap<>();
        {
            obj.put("key1", 200);
            obj.put("key2", 11);
            obj.put("key3", 33);
        }

//        Map 转 Json
        String jsonResult=JSON.toJSONString(obj);
        System.out.println("Map转JSON结果:"+jsonResult);

//       方法一、 Json 转 Map<String,Integer>
        Map<String, Integer> params = JSON.parseObject(jsonResult, new TypeReference<Map<String, Integer>>(){});
        System.out.println("params: "+params);
//        方法二、Json 转 Map
        Map<String, Integer> params1 = JSONObject.parseObject(jsonResult, new TypeReference<Map<String,Integer>>(String.class,Integer.class){});
        System.out.println("params1: "+params1);
        System.out.println(params+" 各参数类型******");

        for (Map.Entry<String,Integer> entry:params.entrySet()){
            System.out.println(entry.getKey() instanceof String);
            System.out.println(entry.getValue() instanceof Integer);
        }
        System.out.println(params1+" 各参数类型******");
        for (Map.Entry<String,Integer> entry:params1.entrySet()){
            System.out.println(entry.getKey() instanceof String);
            System.out.println(entry.getValue() instanceof Integer);
        }
        //方法三、 Json 转 Map
        System.out.println("新的方法:"+parseToMap(jsonResult,String.class,Integer.class));

        //TypeReference protected类型 通过 实例化子类 或者 匿名类 的方式获取实例对象
        TypeReference<Map<String,Integer>> typeReference=new TypeSon();
        System.out.println("TypeReference 子类获取的类型:"+typeReference.getType().getTypeName());
    }
    // 继承自 TypeReference<T> 的子类
    static class TypeSon extends TypeReference<Map<String,Integer>>{}

    // json 转 Map 方法,带泛型的 Map
    public static <k,v> Map<k,v> parseToMap(String json,Type k, Type v){
        return JSON.parseObject(json,new TypeReference<Map<k,v>>(k,v){});
    }

 

 
        
        // List 与 Json 互转
        List<Person> people=new ArrayList<>();
        people.add(new Person("zhangsan"));
        people.add(new Person("zhangsan22"));
        people.add(new Person("zhangsan33"));

        String peopleJson= JSON.toJSONString(people,true);
        System.out.println("peopleJson:"+peopleJson);

        List<Person> people1=JSON.parseArray(peopleJson,Person.class);
        System.out.println("people1:"+people1);

        List<String> pp=List.of("11","22","33");
        String ppJson=JSON.toJSONString(pp,true);
        System.out.println("ppJson:"+ppJson);

        List<String> pp1=JSON.parseArray(ppJson,String.class);
        System.out.println("pp1:"+pp1);
        System.out.println("pp:"+pp);

        List<Integer> ppp=List.of(11,22,33);
        String pppJson=JSON.toJSONString(ppp,true);
        System.out.println("pppJson:"+pppJson);

//        List<Integer> ppp1=JSON.parseArray(pppJson,Integer.class);
        List<Object> ppp1=JSON.parseArray(pppJson);
        System.out.println("ppp1:"+ppp1);
        System.out.println("ppp:"+ppp);
        for (Object i:ppp1){
            System.out.println(i instanceof Integer);
            System.out.println(i instanceof String);
        }
    }
   }

 

posted @ 2020-08-11 16:15  小金儿  阅读(359)  评论(0编辑  收藏  举报