JSON简介(java中的json库使用)

JSON数据格式

JSON(JavaScript Object Notation)js对象标记语言,是一种轻量级的数据交换格式,他是基于ECMAScript的一个子集,采用完全独立于编程语言的文本格式来存储表示数据,简洁和清晰的层次结构使得JSON成为理想的数据交换语言,易于人阅读和编写,同样也易于机器的解析和生成,并且有效的提升网络传输的效率

官网:https://www.json.org/json-zh.html

JSON的三种数据格式:

类型语法说明
对象类型{key1:value,key2:value···}key是字符串类型,value类型任意
数组/集合类型[value1,value2,value3···]把它当做成一个数组,value类型任意
混合类型[{key1:value···},{key2:value···}]数组中放对象
{key1:[value1,value2111],key2[]···}对象中放数组
合理包裹嵌套对象类型和数组类型

JSON转换工具:

json的转换工具是通过java封装好的一些jar工具包,直接将java对象或集合转换成json格式的字符串。

工具名称说明
jsonlibjava类库,需要导入的jar包较多
GsonGoogle提供的一个json转换工具
fastjson阿里巴巴提供的json转换工具,性能较高
jackson知名、好用的json库,Spring默认使用的JSON\XML解析器

jackson

使用步骤:导入jar包,创建创建json转换对象,调用对象中的方法,进行json格式和对象之间的相互转换

		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
		  <groupId>com.fasterxml.jackson.core</groupId>
		  <artifactId>jackson-databind</artifactId>
		  <version>2.11.3</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
		<dependency>
		  <groupId>com.fasterxml.jackson.core</groupId>
		  <artifactId>jackson-core</artifactId>
		  <version>2.11.3</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
		<dependency>
		  <groupId>com.fasterxml.jackson.core</groupId>
		  <artifactId>jackson-annotations</artifactId>
		  <version>2.11.3</version>
		</dependency>

两个常用类:

  • ObjectMapper:是jackson类库中的主要类,他提供了将java对象和json格式字符串相互转换的功能,序列化和反序列化
  • TypeReference:用来指定反序列化的类型

对象类型转换:

    @Test
    public void test1() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Student student = new Student(1001,"小明",20);
        //将对象转换为json格式字符串
        String jsonStr = objectMapper.writeValueAsString(student);
        System.out.println(jsonStr);//返回结果:{"id":1001,"name":"小明","age":20}
        //再将json字符串转换为对象
        Student student1 = objectMapper.readValue(jsonStr,Student.class);
        System.out.println(student1.getName());
    }

集合类型的转换:

list:

    @Test
    public void test3() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        List<Student> list = new ArrayList<>();
        list.add(new Student(1001,"小明",20));
        list.add(new Student(1002,"小红",22));
        list.add(new Student(1003,"小强",21));
        String jsonStr = objectMapper.writeValueAsString(list);
        //[{"id":1001,"name":"小明","age":20},{"id":1002,"name":"小红","age":22},{"id":1003,"name":"小强","age":21}]
        System.out.println(jsonStr);

        List<Student> list1 = objectMapper.readValue(jsonStr, new TypeReference<>() {});
        System.out.println(list1.get(0));

    }

map:

    @Test
    public void test4() throws JsonProcessingException {
        Map<String,Student> map = new HashMap<>();
        map.put("stu1",new Student(1001,"小明",20));
        map.put("stu2",new Student(1002,"小红",21));
        map.put("stu3",new Student(1003,"小强",22));
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = objectMapper.writeValueAsString(map);
        //{"stu2":{"id":1002,"name":"小红","age":21},"stu3":{"id":1003,"name":"小强","age":22},"stu1":{"id":1001,"name":"小明","age":20}}
        System.out.println(jsonStr);

        Map<String,Student> map1 = objectMapper.readValue(jsonStr, new TypeReference<Map<String, Student>>() {});
        System.out.println(map1.get("stu1"));
    }

fastjson

依赖包

		<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
		<dependency>
		  <groupId>com.alibaba</groupId>
		  <artifactId>fastjson</artifactId>
		  <version>1.2.75</version>
		</dependency>

常用方法:

  • JSON.toJSONString(obj) 将一个对象序列化为一个json格式的字符串
  • JSON.parseObject(jsonStr,obj.class) 将一个json格式的字符串反序列化为指定对象类型

fastjson中也提供了TypeReference类

示例:

    @Test
    public void test4(){
        Student student = new Student(1001,"小明",20);
        
        String jsonStr = JSON.toJSONString(student);
        System.out.println(jsonStr);//返回结果:{"id":1001,"name":"小明","age":20}
        
        //再将json字符串转换为对象
        Student student1 = JSON.parseObject(jsonStr,Student.class);
        System.out.println(student1.getName());
    }


    @Test
    public void test5() {
        List<Student> list = new ArrayList<>();
        list.add(new Student(1001,"小明",20));
        list.add(new Student(1002,"小红",22));
        list.add(new Student(1003,"小强",21));
        
        String jsonStr = JSON.toJSONString(list);
        //[{"id":1001,"name":"小明","age":20},{"id":1002,"name":"小红","age":22},{"id":1003,"name":"小强","age":21}]
        System.out.println(jsonStr);

        List<Student> list1 = JSON.parseObject(jsonStr, new TypeReference<>(){});
        System.out.println(list1.get(0));
    }

    @Test
    public void test6(){
        Map<String,Student> map = new HashMap<>();
        map.put("stu1",new Student(1001,"小明",20));
        map.put("stu2",new Student(1002,"小红",21));
        map.put("stu3",new Student(1003,"小强",22));

        String jsonStr = JSON.toJSONString(map);
        //{"stu2":{"id":1002,"name":"小红","age":21},"stu3":{"id":1003,"name":"小强","age":22},"stu1":{"id":1001,"name":"小明","age":20}}
        System.out.println(jsonStr);

        Map<String,Student> map1 = JSON.parseObject(jsonStr,new TypeReference<>(){});
        System.out.println(map1.get("stu1"));
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值