Jackson chapter of java learning

Jackson chapter of java learning

0x00 Preface

The content of this article is relatively simple and simple to record.

0x01 Jason overview

Here is a copy of the explanation of Baidu Encyclopedia. In fact, the content can be summarized in one sentence.

In Java, you need to parse JSON, and you need to use a JSON parser.

0x02 Jackson parser

Common JSON parsers in Java include

Jsonlib,Gson,fastjson,jackson

So here's Jackson for a demonstration.

common method

1. readValue(json字符串数据,Class)      json转换为java对象

2.writeValue(参数1,obj):
 参数1:
 
File:将obj对象转换为JSON字符串,并保存到指定的文件中

Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中

OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中

3.writeValueAsString(obj):将对象转为json字符串


Notes:

1. @JsonIgnore:排除属性。

2. @JsonFormat:属性值得格式化

Object to JSON

Here you also need to define an entity class, which is not written here.

package com.test.domain;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class jsonDemo {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("xiaoming");
        person.setSex("name");
        person.setAge(18);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String s = objectMapper.writeValueAsString(person);
            System.out.println(s);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }


    }
}

Display data:

List collection conversion JSON

package com.test.domain;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

public class jsonDemo {
    public static void main(String[] args) {
        Person p1= new Person();
        p1.setName("xiaoming");
        p1.setSex("name");
        p1.setAge(18);
        Person p2= new Person();
        p2.setName("xiaoming");
        p2.setSex("name");
        p2.setAge(18);
        ObjectMapper objectMapper = new ObjectMapper();
        List<Person> list = new ArrayList<>();
        list.add(p1);
        list.add(p2);

        try {
            String s = objectMapper.writeValueAsString(list);
            System.out.println(s);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

JSON to Java object

package com.test.domain;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class jsonDemo {
    public static void main(String[] args) throws IOException {
       String json = "{\"sex\":\"男\",\"age\":\"18\",\"name\":\"xiaoming\"}";
        ObjectMapper objectMapper = new ObjectMapper();
        Person person = objectMapper.readValue(json,Person.class);
        System.out.println(person.toString());
    }
}

0x03 end

We are going to update the Java framework content later.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>