using java jackson generation and parsing JSON

using java jackson generation and parsing JSON

1. Package guide

 

 2. generation and parsing json json  

Package Test; 

Import com.fasterxml.jackson.core.JsonProcessingException;
 Import com.fasterxml.jackson.databind.ObjectMapper;
 Import domain.Person;
 Import org.junit.Test; 

Import java.io.File;
 Import java.io.FileWriter ;
 import java.io.IOException;
 import Classes in java.util *. ; 

/ ** 
 * 2. the Java objects into the JSON 
 * 1. use steps: 
 * 1. import jackson relevant jar package 
 * 2. Create Jackson core object ObjectMapper 
 *. 3 Related ObjectMapper method calls for conversion  
 * 1. conversion method:
 * * writeValue (parameter 1, obj): 
 * Parameters. 1: 
 * File: converting JSON string object obj, and saved in the specified file 
 * Writer: converting JSON string object obj, and padding data json the character output stream 
 * OutputStream: converting JSON string object obj, and filled into json data byte output stream 
 * * writeValueAsString (obj): string object into json 
 * using jackSon generation parsing jsom 
 * @ 2019/10/5 15:09 DATE 
 * / 
public  class Test1 { 
    @Test // generate JSON 
    public  void Test () throws IOException { 
        the Person P = new new the Person ();
        p.setName ( "parse json" ); 
        p.setAge (13 ); 
        p.setGender ( "male" ); 
        ObjectMapper Mapper = new new ObjectMapper (); // objects to create objmapper of 
        String String = mapper.writeValueAsString (the p-);
         / *     * mapper.writeValue (parameter 1, obj): 1.File: converting JSON string object obj, and saved in the specified file 
         * 2.Writer: converting JSON string object obj, and to fill the character json data output stream 
         * 3.OutputStream: the string into a JSON object obj, and filled into json data output stream of bytes * / 
        System.out.println (string); // { "name": "parse json", "age": 13 , "gender ":" male "} 

        //  1.File: converting JSON string object obj, and saved in the specified file 
        mapper.writeValue ( new new File ( "D: //a.txt" ), P);
         //    2.Writer: the object obj into a JSON string, and the character fill json data output stream 
        mapper.writeValue ( new new FileWriter ( "D: //b.txt" ), P); 
    } 
    @Test // json annotation 
    public  void test1 () throws {JsonProcessingException 
        the Person P = new new the Person (); 
        p.setName ( "parsing JSON" ); 
        p.setAge ( 13 is ); 
        p.setGender ( "M" ); 
        p.setDate (new new a Date ()); 
        ObjectMapper Mapper = new new ObjectMapper (); // create objects objmapper 
        String String = mapper.writeValueAsString (P); 
        System.out.println (String); 
    } 
    @Test // JSON complex transformations 
    public  void test2 () throws JsonProcessingException { 
        the Person P = new new the Person (); 
        p.setName ( "parsing JSON" ); 
        p.setAge ( 13 is ); 
        p.setGender ( "M" ); 
        p.setDate ( new new a Date ());
 
        the Person P1 =new Person();
        p.setName("解析json");
        p.setAge(13);
        p.setGender("男");
        p.setDate(new Date());

        Person p2=new Person();
        p.setName("解析json");
        p.setAge(13);
        p.setGender("男");
        p.setDate(new Date());
        //集合
        List<Person> PS=new ArrayList<>();
        PS.add(p);
        PS.add(p1);
        PS.add(p2);
        ObjectMapper mapper=new ObjectMapper();//先创建objmapper的对象
        String string = mapper.writeValueAsString(PS);
        System.out.println(string);
    }
    @Test//json  MAP生成json
    public void test3() throws JsonProcessingException {
        //创建map对象
        Map<String,Object> map=new HashMap<>();
        map.put("name","map");
        map.put("age",11);
        map.put("gender","男");
        Mapper ObjectMapper= New new ObjectMapper (); // create objects objmapper 
        String String = mapper.writeValueAsString (Map); 
        System.out.println (String); 
    } 
    @Test // JSON string into a java object 
    public  void Test4 () throws IOException { 
        String JSON = "{\" Gender \ ": \" M \ ", \" name \ ": \" Map \ ", \" Age \ ":. 11}" ; 
        ObjectMapper Mapper = new new ObjectMapper (); 
        the Person the Person = mapper.readValue (json, the Person. class ); // With the object can call getXx and setXxx method of 
        System.out.println(person);// print a person object Person {name = 'map', age = 11, gender = ' M', DATE = null}
         // This is a json string { "name": "parse json", "age": 13, "gender": "male"} 
    } 
}

 3.person class

package domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.Date;

public class Person {
    private String name;
    private int age;
    private String gender;
    //@JsonIgnore 转换时忽略 date字段
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date date;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", date=" + date +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

 

Guess you like

Origin www.cnblogs.com/july7/p/11626901.html