Json数据和java对象的相互转换

Json数据和java对象的相互转换,第1张

Json数据和java对象的相互转换

文章目录
  • 一、Json转化成java对象
  • 二、java对象转换json


提示:以下是本篇文章正文内容,下面案例可供参考

json解析器:jackson
导入jackson.jar
github下载地址

一、Json转化成java对象
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;


public class JacksonTest {
    //json字符串转化为java对象
    @Test
    public void Tset01() throws Exception {
        //1.初始化json字符串
        String json = "{"gender":"男","name":"张三","age":20}";

        //2.创建ObjectMapper
        ObjectMapper objectMapper = new ObjectMapper();

        //3.转换为java对象,user对象
        User user = objectMapper.readValue(json, User.class);
        System.out.println(user);
    }
}

二、java对象转换json

user对象

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

import java.util.Date;

public class User {
    private String name;
    private int age;
    private String gender;
    // @JsonIgnore //json解析忽略该类型
    @JsonFormat(pattern = "yyyy-MM-dd") //属性值的格式化
    private Date birthday;

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public User() {
    }

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

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

json解析

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JacksonTest {
    @Test
    public void Tset01() throws Exception {
        User  user = new User();
        user.setName("张三");
        user.setAge(23);
        user.setGender("男");

        //1.创建Jackson的核心对象 ObjectMapper
        ObjectMapper objectMapper = new ObjectMapper();
        //2.转换
        
        // {"name":"张三","age":23,"gender":"男"}
        String json = objectMapper.writevalueAsString(user);
        System.out.println(json);

        // writevalue,将数据写到文件中
        //objectMapper.writevalue(new File(""),p);

        //writevalue. 将数据关联到Writer中
        //objectMapper.writevalue(new FileWriter(""),user);
    }
    //注解解析json
    @Test
    public void Tset02() throws Exception{
        User  user1 = new User();
        user1.setName("张三");
        user1.setAge(23);
        user1.setGender("男");
        user1.setBirthday(new Date());

        User  user2 = new User();
        user2.setName("张三");
        user2.setAge(23);
        user2.setGender("男");
        user2.setBirthday(new Date());

        User  user3 = new User();
        user3.setName("张三");
        user3.setAge(23);
        user3.setGender("男");
        user3.setBirthday(new Date());

//        ArrayList users = new ArrayList<>();
//        users.add(user1);
//        users.add(user2);
//        users.add(user3);
        Map map = new HashMap();
        map.put("name","张三");
        map.put("age",20);
        map.put("gender","男");
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writevalueAsString(map);
        System.out.println(json);

    }


}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4670715.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存