java对象转json

json解析器:

  常见的解析器:Jsonlib,Gson,fastjson,jackson

使用步骤:

  1、导入jackson的相关jar包

  2、创建Jackson核心对象 ObjectMapper

  3、调用ObjectMapper的相关方法进行转换

转换方法:

writeValue(参数1,obj);
参数1:
File:将obj对象转换诶JSON字符串,并保存到指定的文件中
Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
writeValueAsString(obj):将对象转为json字符串

Person对象实体类:

public class Person {
    private String name;
    private int age;
    private String gender;

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

    public Person() {
    }

    public Person(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    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;
    }
}

java转json字符串

//java对象转为JSON字符串
    @Test
    public void test1() throws Exception {
        //1、创建Person对象
        Person p = new Person();
        p.setName("张三");
        p.setAge(20);
        p.setGender("男");

        //2、创建Jackson的核心对象, ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
        //3、转换
        /*
            转换:
                writeValue(参数1,obj);
                    参数1:
                        File:将obj对象转换诶JSON字符串,并保存到指定的文件中
                        Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
                        OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
                writeValueAsString(obj):将对象转为json字符串

         */
        String json = mapper.writeValueAsString(p);
        System.out.println(json);

        //writeValue:将数据写到文件中
//        mapper.writeValue(new File("G://r.txt"),p);
        mapper.writeValue(new FileWriter("G://r.txt"),p);
    }

 

 

 

 

java对象转json—注解

@JsonIgnore:排除属性

Person类:

public class Person {
    private String name;
    private int age;
    private String gender;

    @JsonIgnore
    private Date birthday;

测试:

@Test
    public void test2() throws Exception {
        //1、创建Person对象
        Person p = new Person();
        p.setName("张三");
        p.setAge(20);
        p.setGender("男");
        p.setBirthday(new Date());
        //转换
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(p);
        System.out.println(json);
    }

 

 

 

@JsonFormat:格式化属性

public class Person {
    private String name;
    private int age;
    private String gender;

    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date birthday;

再次运行性测试方法

 

 

 

 

 

 

java对象转json—List&Map

List集合:

@Test
    public void test3() throws Exception {
        //1、创建Person对象
        Person p = new Person();
        p.setName("张三");
        p.setAge(20);
        p.setGender("男");
        p.setBirthday(new Date());
        Person p1 = new Person();
        p1.setName("张三");
        p1.setAge(20);
        p1.setGender("男");
        p1.setBirthday(new Date());
        Person p2 = new Person();
        p2.setName("张三");
        p2.setAge(20);
        p2.setGender("男");
        p2.setBirthday(new Date());

        //创建list集合
        List<Person> list = new ArrayList<>();
        list.add(p);
        list.add(p1);
        list.add(p2);

        //转换
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(list);
        System.out.println(json);
    }

 

Map集合:

    @Test
    public void test4() throws Exception {
        //1、创建Map对象
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age","23");
        map.put("gender","男");
        //转换
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(map);
        System.out.println(json);
    }

 

版权声明:本文为xjw12345原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xjw12345/p/16609309.html