JSON与各种类型互转的集锦

目录

目标

POM依赖

String转其他类型

String(JSON格式)转JSONObject

String(JSON格式)转JSONArray

String(JSON格式)转Bean

String(JSON格式)转Map

String(JSON格式)转ListMap

String(JSON格式)转ListBean

JSONObject转其他类型

JSONObject转String(JSON格式)

JSONObject转Map

JSONObject转Bean

JSONArray转其他类型

JSONArray转ListMap

JSONArray转ListBean

Map转其他类型

Map转String(JSON格式)

Map转Bean

Map转JSONObject

ListMap转其他类型

ListMap转String(JSON格式)

ListMap转JSONArray

ListBean转其他类型

ListBean转String(JSON格式)

ListBean转JSONArray

Bean转其他类型 

Bean转Map

Bean转String(JSON格式)

Bean转JSONObject


目标

不使用循环的方式,枚举JSON、String、Map、Bean、List、List之间互转的方法。


POM依赖

		
			com.alibaba
			fastjson
			1.2.9
		
		
			com.google.code.gson
			gson
			2.8.0
		
        
			commons-beanutils
			commons-beanutils
			1.9.3
		
		
			com.fasterxml.jackson.core
			jackson-databind
			2.13.0
		

String转其他类型

String(JSON格式)转JSONObject

    public void stringToJSONObject() {
        String str = "{\"create_time\":\"2022-01-01 23:59:59\",\"name\":\"张三\",\"id\":123456789}";
        /*方法一*/
        JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(str);
        System.out.println(jsonObject);
        /*方法二*/
        JSONObject jsonObject1 = com.alibaba.fastjson2.JSON.parseObject(str, JSONObject.class);
        System.out.println(jsonObject1);
        /*方法三*/
        com.alibaba.fastjson2.JSONObject jsonObject2 = com.alibaba.fastjson2.JSONObject.parseObject(str);
        System.out.println(jsonObject2);
        /*方法四*/
        com.alibaba.fastjson2.JSONObject jsonObject3 = com.alibaba.fastjson2.JSONObject.parseObject(str, com.alibaba.fastjson2.JSONObject.class);
        System.out.println(jsonObject3);
    }

String(JSON格式)转JSONArray

    public void stringToJsonArray(){
        String json = "[{\"create_time\":\"2022-01-01 23:59:59\",\"name\":\"张三\",\"id\":123456789},{\"create_time\":\"2022-01-01 23:59:59\",\"name\":\"张三\",\"id\":123456789}]";
        /*方法一*/
        com.alibaba.fastjson2.JSONArray jsonArray = com.alibaba.fastjson2.JSONArray.parseArray(json);
        System.out.println(jsonArray);
        /*方法二(不推荐)*/
        com.google.gson.Gson gson = new Gson();
        JSONArray jsonArray2 = gson.fromJson(json, new TypeToken() {
        }.getType());
        System.out.println(jsonArray2);
        /*方法三*/
        com.alibaba.fastjson2.JSONArray jsonArray3 = new com.alibaba.fastjson2.JSONArray(json);
        System.out.println(jsonArray3);
    }

String(JSON格式)转Bean

    public void stringToBean() {
        String str = "{\"id\":123456789,\"name\":\"张三\",\"sex\":true}";
        /*方法一*/
        Student student = com.alibaba.fastjson2.JSON.parseObject(str, Student.class);
        System.out.println(student.getId());
        System.out.println(student.isSex());
        System.out.println(student.getName());

        System.out.println("---------------------------------------------");
        /*方法二*/
        Gson gson = new Gson();
        Student student2 = gson.fromJson(str, new TypeToken() {
        }.getType());
        System.out.println(student.getId());
        System.out.println(student.isSex());
        System.out.println(student.getName());
    }

String(JSON格式)转Map

    /**
     * String(JSON格式)转Map
     */
    public void stringToMap(){
        String str="{\"create_time\":\"2022-01-01 23:59:59\",\"name\":\"张三\",\"id\":123456789}";
        /*方法一*/
        Gson gson = new Gson();
        Map map= gson.fromJson(str, new TypeToken>() {
        }.getType());
        System.out.println(map);
        /*方法二*/
        Map map2 = com.alibaba.fastjson2.JSON.parseObject(str, Map.class);
        System.out.println(map2);
    }

String(JSON格式)转ListMap

    public void stringToList() {
        String json = "[{\"create_time\":\"2022-01-01 23:59:59\",\"name\":\"张三\",\"id\":123456789},{\"create_time\":\"2022-01-01 23:59:59\",\"name\":\"张三\",\"id\":123456789}]";
        /*方法一*/
        List> list = com.alibaba.fastjson2.JSON.parseObject(json, List.class);
        list.forEach(map->{
            map.forEach((k,v)->{
                System.out.println(k+"="+v);
            });
        });
        System.out.println("---------------------------------------------");
        /*方法二*/
        Gson gson = new Gson();
        List> list2= gson.fromJson(json, new TypeToken>>() {
        }.getType());
        list2.forEach(map->{
            map.forEach((k,v)->{
                System.out.println(k+"="+v);
            });
        });
    }

String(JSON格式)转ListBean

    public void stringToListBean(){
        String str="[{\"id\":123456789,\"name\":\"张三\",\"sex\":true},{\"id\":123456780,\"name\":\"李四\",\"sex\":false},{\"id\":123456781,\"name\":\"王五\",\"sex\":true}]";
        com.google.gson.Gson gson = new Gson();
        List list = gson.fromJson(str, new TypeToken>() {
        }.getType());
        list.forEach(
                student -> {
                    System.out.println(student.getId());
                    System.out.println(student.getName());
                    System.out.println(student.isSex());
                    System.out.println("--------------------");
                }
        );
    }

JSONObject转其他类型

JSONObject转String(JSON格式)

    public void jsonObjectToString() {
        String str = "{\"create_time\":\"2022-01-01 23:59:59\",\"name\":\"张三\",\"id\":123456789}";
        JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(str);
        /*方法一*/
        String s = jsonObject.toString();
        System.out.println(s);
        /*方法二*/
        String s1 = com.alibaba.fastjson2.JSONObject.toJSONString(jsonObject);
        System.out.println(s1);
    }

JSONObject转Map

    public void jsonObjectToMap() {
        String str = "{\"create_time\":\"2022-01-01 23:59:59\",\"name\":\"张三\",\"id\":123456789}";
        JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(str);
        /*方法一*/
        Map map = JSON.parseObject(jsonObject.toJSONString(), Map.class);
        System.out.println(map);
        /*方法二*/
        Map map1 = com.alibaba.fastjson2.JSONObject.parseObject(jsonObject.toJSONString(), Map.class);
        System.out.println(map1);
        /*方法三*/
        Map map2 = JSON.parseObject(jsonObject.toJSONString(), Map.class);
        System.out.println(map2);
        /*方法四*/
        Gson gson = new Gson();
        Map map3 = gson.fromJson(jsonObject.toJSONString(), new TypeToken>() {
        }.getType());
        System.out.println(map3);
    }

JSONObject转Bean

    public void jsonObjectToBean() {
        String str = "{\"id\":123456789,\"name\":\"张三\",\"sex\":true}";
        com.alibaba.fastjson2.JSONObject jsonObject = JSON.parseObject(str);
        System.out.println(jsonObject);
        /*方法一*/
        Student student = com.alibaba.fastjson2.JSON.parseObject(jsonObject.toString(), Student.class);
        System.out.println(student.getId());
        System.out.println(student.isSex());
        System.out.println(student.getName());

        System.out.println("---------------------------------------------");
        /*方法二*/
        Student student1 = com.alibaba.fastjson2.JSONObject.parseObject(jsonObject.toJSONString(), Student.class);
        System.out.println(student1.getId());
        System.out.println(student1.isSex());
        System.out.println(student1.getName());
        System.out.println("---------------------------------------------");
        /*方法三*/
        Student student2 = JSON.parseObject(jsonObject.toJSONString(), Student.class);
        System.out.println(student2.getId());
        System.out.println(student2.isSex());
        System.out.println(student2.getName());
        System.out.println("---------------------------------------------");
        /*方法四*/
        Gson gson = new Gson();
        Student student3 = gson.fromJson(jsonObject.toString(), new TypeToken() {
        }.getType());
        System.out.println(student3.getId());
        System.out.println(student3.isSex());
        System.out.println(student3.getName());
    }

JSONArray转其他类型

JSONArray转ListMap

    public void jsonArrayToList() {
        Map map = new HashMap<>();
        map.put("id", 123456789);
        map.put("name", "张三");
        map.put("create_time", "2022-01-01 23:59:59");
        Map map2 = new HashMap<>();
        map2.put("id", 123456789);
        map2.put("name", "张三");
        map2.put("create_time", "2022-01-01 23:59:59");
        ArrayList list = new ArrayList<>();
        list.add(map);
        list.add(map2);
        com.alibaba.fastjson.JSONArray jsonArray = new com.alibaba.fastjson.JSONArray(list);
        /*方法一*/
        com.google.gson.Gson gson = new Gson();
        List> listMap = gson.fromJson(jsonArray.toJSONString(), new TypeToken>>() {
        }.getType());
        listMap.forEach(
                item -> {
                    item.forEach(
                            (k, v) -> {
                                System.out.println(k + "=" + v);
                            }
                    );
                }
        );
        System.out.println("--------------------------");
        /*方法二(不推荐,之所以写出来是因为看到网上很多博客用了两层循环嵌套去实现这个功能,个人认为不必如此麻烦。)*/
        List> result = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            Map m = JSON.parseObject(jsonArray.getJSONObject(i).toJSONString(), Map.class);
            result.add(map);
        }
        result.forEach(
                item -> {
                    item.forEach(
                            (k, v) -> {
                                System.out.println(k + "=" + v);
                            }
                    );
                }
        );
        System.out.println("--------------------------");
        /*方法三(推荐)*/
        List> list1 = com.alibaba.fastjson2.JSON.parseObject(jsonArray.toJSONString(), List.class);
        list1.forEach(
                item -> {
                    item.forEach(
                            (k, v) -> {
                                System.out.println(k + "=" + v);
                            }
                    );
                }
        );
        System.out.println("--------------------------");
        /*方法四(推荐)*/
        List> list2 = com.alibaba.fastjson2.JSONObject.parseObject(jsonArray.toJSONString(), List.class);
        list2.forEach(
                item -> {
                    item.forEach(
                            (k, v) -> {
                                System.out.println(k + "=" + v);
                            }
                    );
                }
        );
    }

JSONArray转ListBean

    public void jsonArrayToListBean() {
        Map map = new HashMap<>();
        map.put("id", 123456789);
        map.put("name", "李四");
        map.put("sex", false);
        Map map2 = new HashMap<>();
        map2.put("id", 123456789);
        map2.put("name", "张三");
        map2.put("sex", true);
        ArrayList list = new ArrayList<>();
        list.add(map);
        list.add(map2);
        com.alibaba.fastjson.JSONArray jsonArray = new com.alibaba.fastjson.JSONArray(list);
        /*方法一(推荐)*/
        com.google.gson.Gson gson = new Gson();
        List listMap = gson.fromJson(jsonArray.toJSONString(), new TypeToken>() {
        }.getType());
        listMap.forEach(
                student->{
                    System.out.println(student.getId());
                    System.out.println(student.isSex());
                    System.out.println(student.getName());
                }
        );
        System.out.println("--------------------------");
        /*方法二(不推荐,之所以写出来是因为看到网上很多博客用了两层循环嵌套去实现这个功能,个人认为不必如此麻烦。)*/
        List result = new ArrayList<>();
        for(int i=0;i{
                    System.out.println(student.getId());
                    System.out.println(student.isSex());
                    System.out.println(student.getName());
                }
        );
    }

Map转其他类型

Map转String(JSON格式)

    public void mapToString() {
        Map map = new HashMap<>();
        map.put("id", 123456789);
        map.put("name", "张三");
        map.put("create_time", "2022-01-01 23:59:59");
        Gson gson = new Gson();
        /*方法一(优于方法二)*/
        String json = gson.toJson(map, Map.class);
        System.out.println(json);
        /*方法二*/
        String json2 = gson.toJson(map);
        System.out.println(json2);
        /*方法三*/
        String str = com.alibaba.fastjson2.JSON.toJSONString(map);
        System.out.println(str);
        /*方法四*/
        String s = com.alibaba.fastjson2.JSONObject.toJSONString(map);
        System.out.println(s);
        /*方法五*/
        String s1 = com.alibaba.fastjson.JSON.toJSONString(map);
        System.out.println(s1);
        /*方法五*/
        String s2 = com.alibaba.fastjson.JSONObject.toJSONString(map);
        System.out.println(s2);
    }

Map转Bean

    public void mapToBean() {
        HashMap map = new HashMap<>();
        map.put("id",123456789L);
        map.put("name","张三");
        map.put("sex",true);
        /*方法一:使用commons-beanutils,注意:这里的id长度过长要加L表示Long类型。*/
        try {
            Student student = mapToBean(map, Student.class);
            System.out.println(student.getId());
            System.out.println(student.isSex());
            System.out.println(student.getName());
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }
        /*方法二:使用jackson-databind,这里的id可以不用加L。*/
        ObjectMapper mapper = new ObjectMapper();
        try {
            Student student = mapper.readValue(com.alibaba.fastjson2.JSON.toJSONString(map), Student.class);
            System.out.println(student.getId());
            System.out.println(student.isSex());
            System.out.println(student.getName());
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

Map转JSONObject

    public void mapToJsonObject() {
        Map map = new HashMap<>();
        map.put("id", 123456789);
        map.put("name", "张三");
        map.put("create_time", "2022-01-01 23:59:59");
        Gson gson = new Gson();
        /*方法一(优于方法二)*/
        JsonElement jsonElement = gson.toJsonTree(map, Map.class);
        com.google.gson.JsonObject jsonObject = jsonElement.getAsJsonObject();
        System.out.println(jsonObject);
        /*方法二*/
        JsonElement jsonElement2 = gson.toJsonTree(map);
        com.google.gson.JsonObject jsonObject2 = jsonElement2.getAsJsonObject();
        System.out.println(jsonObject2);
        /*方法三*/
        com.alibaba.fastjson.JSONObject jsonObject3 = new com.alibaba.fastjson.JSONObject(map);
        System.out.println(jsonObject3);
        /*方法四*/
        String s = com.alibaba.fastjson2.JSON.toJSONString(map);
        JSONObject jsonObject4 = com.alibaba.fastjson.JSONObject.parseObject(s);
        System.out.println(jsonObject4);
        /*方法五*/
        com.alibaba.fastjson2.JSONObject jsonObject1 = new com.alibaba.fastjson2.JSONObject(map);
        System.out.println(jsonObject1);
        /*方法六*/
        com.alibaba.fastjson2.JSONObject jsonObject5 = com.alibaba.fastjson2.JSON.parseObject(JSON.toJSONString(map));
        System.out.println(jsonObject5);
    }

ListMap转其他类型

ListMap转String(JSON格式)

    public void listToString() {
        Map map = new HashMap<>();
        map.put("id", 123456789);
        map.put("name", "张三");
        map.put("create_time", "2022-01-01 23:59:59");
        Map map2 = new HashMap<>();
        map2.put("id", 123456789);
        map2.put("name", "张三");
        map2.put("create_time", "2022-01-01 23:59:59");
        ArrayList list = new ArrayList<>();
        list.add(map);
        list.add(map2);
        /*方法一*/
        String str = com.alibaba.fastjson2.JSON.toJSONString(list);
        System.out.println(str);
        /*方法二*/
        String s = com.alibaba.fastjson.JSONObject.toJSONString(list);
        System.out.println(s);

        Gson gson = new Gson();
        /*方法三(优于方法四)*/
        String json = gson.toJson(list,List.class);
        System.out.println(json);
        /*方法四*/
        String json2 = gson.toJson(list);
        System.out.println(json2);
    }

ListMap转JSONArray

    public void listToJsonArray() {
        Map map = new HashMap<>();
        map.put("id", 123456789);
        map.put("name", "张三");
        map.put("create_time", "2022-01-01 23:59:59");
        Map map2 = new HashMap<>();
        map2.put("id", 123456789);
        map2.put("name", "张三");
        map2.put("create_time", "2022-01-01 23:59:59");
        ArrayList list = new ArrayList<>();
        list.add(map);
        list.add(map2);
        /*方法一*/
        JSONArray jsonArray = new com.alibaba.fastjson.JSONArray(list);
        for (Object o : jsonArray) {
            System.out.println(o);
        }
        /*方法二*/
        com.alibaba.fastjson.JSONArray jsonArray1 = com.alibaba.fastjson.JSONArray.parseArray(com.alibaba.fastjson2.JSON.toJSONString(list));
        for (Object o : jsonArray1) {
            System.out.println(o);
        }
        /*方法三*/
        Gson gson = new Gson();
        JsonElement jsonElement = gson.toJsonTree(list, com.alibaba.fastjson.JSONArray.class);
        JsonArray asJsonArray = jsonElement.getAsJsonArray();
        for (JsonElement element : asJsonArray) {
            System.out.println(element);
        }
        /*方法四*/
        com.alibaba.fastjson2.JSONArray jsonArray2 = new com.alibaba.fastjson2.JSONArray(list);
        for (Object o : jsonArray2) {
            System.out.println(o);
        }
    }

ListBean转其他类型

ListBean转String(JSON格式)

    public void listBeanToString() {
        List list = new ArrayList<>();
        list.add(new Student(123456789L, "张三", true));
        list.add(new Student(123456780L, "李四", false));
        list.add(new Student(123456781L, "王五", true));
        /*方法一*/
        String s = com.alibaba.fastjson2.JSON.toJSONString(list);
        System.out.println(s);
        /*方法二*/
        Gson gson = new Gson();
        String s1 = gson.toJson(list);
        System.out.println(s1);
        /*方法三(优于方法二)*/
        String s2 = gson.toJson(list, List.class);
        System.out.println(s2);
        /*方法四*/
        String s3 = com.alibaba.fastjson.JSON.toJSONString(list);
        System.out.println(s3);
    }

ListBean转JSONArray

    public void listBeanToJsonArray() {
        List list = new ArrayList<>();
        list.add(new Student(123456789L, "张三", true));
        list.add(new Student(123456780L, "李四", false));
        list.add(new Student(123456781L, "王五", true));
        /*方法一*/
        JSONArray jsonArray = new com.alibaba.fastjson.JSONArray(list);
        System.out.println(jsonArray);
        /*方法二*/
        JSONArray jsonArray1 = com.alibaba.fastjson.JSONArray.parseArray(com.alibaba.fastjson2.JSON.toJSONString(list));
        System.out.println(jsonArray1);
        /*方法三*/
        Gson gson = new Gson();
        JsonElement jsonElement = gson.toJsonTree(list, com.alibaba.fastjson.JSONArray.class);
        JsonArray asJsonArray = jsonElement.getAsJsonArray();
        System.out.println(asJsonArray);
        /*方法四*/
        com.alibaba.fastjson2.JSONArray jsonArray2 = new com.alibaba.fastjson2.JSONArray(list);
        System.out.println(jsonArray2);
    }

Bean转其他类型 

Bean转Map

    public void beanToMap(){
        Student stu = new Student(123456789L, "张三", true);
        /*方法一:使用commons-beanutils,不循环只能转Map*/
        try {
            Map map = BeanUtils.describe(stu);
            System.out.println("方法一:"+map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        /*方法二:使用jackson-databind*/
        Map map = new ObjectMapper().convertValue(stu, new com.fasterxml.jackson.core.type.TypeReference>() {});
        System.out.println("方法二:"+map);
        /*方法三:使用commons-beanutils*/
        BeanMap beanMap = BeanMap.create(stu);
        Map map2 = new HashMap<>();
        beanMap.forEach((k, v) -> {
            map2.put(k.toString(), v);
        });
        System.out.println("方法三:"+map2);
    }

Bean转String(JSON格式)

    public void beanToString(){
        Student stu = new Student(123456789L, "张三", true);
        /*方法一*/
        String s = com.alibaba.fastjson2.JSON.toJSONString(stu);
        System.out.println(s);
        /*方法二*/
        String s2 =com.alibaba.fastjson.JSON.toJSONString(stu);
        System.out.println(s2);
        /*方法三*/
        Gson gson = new Gson();
        String s3 = gson.toJson(stu);
        System.out.println(s3);
    }

Bean转JSONObject

    public void beanToJsonObject(){
        Student stu = new Student(123456789L, "张三", true);
        /*方法一*/
        com.alibaba.fastjson2.JSONObject jsonObject = com.alibaba.fastjson2.JSON.parseObject(com.alibaba.fastjson2.JSON.toJSONString(stu), com.alibaba.fastjson2.JSONObject.class);
        System.out.println(jsonObject);
        /*方法二*/
        com.alibaba.fastjson2.JSONObject jsonObject2 = com.alibaba.fastjson2.JSON.parseObject(com.alibaba.fastjson2.JSON.toJSONString(stu), com.alibaba.fastjson2.JSONObject.class);
        System.out.println(jsonObject2);
        /*方法三*/
        Gson gson = new Gson();
        JsonElement jsonElement = gson.toJsonTree(stu);
        JsonObject asJsonObject = jsonElement.getAsJsonObject();
        System.out.println(asJsonObject);
        /*方法四*/
        com.alibaba.fastjson.JSONObject jsonObject3 = com.alibaba.fastjson.JSON.parseObject(com.alibaba.fastjson.JSON.toJSONString(stu), com.alibaba.fastjson.JSONObject.class);
        System.out.println(jsonObject3);
        /*方法五*/
        com.alibaba.fastjson.JSONObject jsonObject4 = com.alibaba.fastjson.JSON.parseObject(com.alibaba.fastjson.JSON.toJSONString(stu), com.alibaba.fastjson.JSONObject.class);
        System.out.println(jsonObject4);
    }

你可能感兴趣的:(json转map,json转list,json转bean,json转对象,对象转json)