JSON转换工具

JSON的处理:

  • JSON(JavaScript Object Notation):是一种轻量级的数据交换格式。

  • 它是基于 ECMAScript 规范的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。

  • 简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。易于人阅读和编写,同时也易于计算机解析和生成,并有效的 提升网络传输效率。

创建格式:

在这里插入图片描述

JSON转换工具的介绍:

  • 我们除了可以在 JavaScript 中来使用 JSON 以外,在 JAVA 中同样也可以使用 JSON。

  • JSON 的转换工具是通过 JAVA 封装好的一些 JAR 工具包。

  • 可以将 JAVA 对象或集合转换成 JSON 格式的字符串,也可以将 JSON 格式的字符串转成 JAVA 对象。

  • Jackson:开源免费的 JSON 转换工具,SpringMVC 转换默认使用 Jackson。

    • 导入 jar 包。
    • 创建核心对象。
    • 调用方法完成转换。

常用方法:
在这里插入图片描述

常用类

在这里插入图片描述

ObjectMapper常用方法

  • 在这里插入图片描述

JSON转换:

对象转 JSON, JSON 转对象:

public class ObjectMapperTest {

    private ObjectMapper mapper = new ObjectMapper();

    @Test
    public void test01() throws Exception {
        User user = new User("韩信", 99);
        //User对象转json
        String json = mapper.writeValueAsString(user);
        System.out.println("json字符串:" + json);

        //json转User对象
        User user2 = mapper.readValue(json, User.class);
        System.out.println("json对象:" + user2);
    }
}

Map转 JSON, JSON 转 Map:

private ObjectMapper mapper = new ObjectMapper();    

@Test
public void test02() throws Exception {

        // map转json
        HashMap<String, String> map = new HashMap<>();
        map.put("姓名", "韩信");
        map.put("性别", "男");
        String json = mapper.writeValueAsString(map);
        System.out.println("json字符串:" + json);

        // json转map
        HashMap user = mapper.readValue(json, HashMap.class);
        System.out.println("json对象:" + user);
    }

@Test
public void test03() throws Exception {
        // map转json
        HashMap<String, User> map = new HashMap<>();
        map.put("打野", new User("韩信", 99));
        map.put("中单", new User("不知去向", 97));
        String json = mapper.writeValueAsString(map);
        System.out.println("json字符串:" + json);

        // json转map  map如果有自定义类型就要用TypeReference
        HashMap<String, User> user = mapper.readValue(json, new TypeReference<HashMap<String, User>>() {});
        System.out.println("json对象:" + user);
    }

List转 JSON, JSON 转 List:

    @Test
    public void test04() throws Exception {
        // List<String>转json
        ArrayList<String> list = new ArrayList<>();
        list.add("上官原地");
        list.add("不知去向");
        String json = mapper.writeValueAsString(list);
        System.out.println("json字符串:" + json);

        // json转List<string>
        ArrayList user = mapper.readValue(json, ArrayList.class);
        System.out.println("json对象:" + user);
    }

    @Test
    public void test05() throws Exception {
        // List<String>转json
        ArrayList<User> list = new ArrayList<>();
        list.add(new User("上官原地", 99));
        list.add(new User("不知去向", 11));
        String json = mapper.writeValueAsString(list);
        System.out.println("json字符串:" + json);

        // json转List<string>
        ArrayList user = mapper.readValue(json, new TypeReference<ArrayList<User>>() {
        });
        System.out.println("json对象:" + user);
    }

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>