javaApi json转换

本文介绍json与对象,list,map之间的转换

导入依赖

		<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.9</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>

lombok介绍

lombok用以生成对象属性对应的set,get方法,toString等方法。
使用时,使用注解
@Data 生成对应的set,get,toString,hashCode方法
@NoArgsConstructor 生成无参构造方法
@AllArgsConstructor 生成全参构造方法

pojo对象

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private Integer age;
}

在这里插入图片描述

pojo与json互转

    @Test
    public void pojo_json() throws IOException {
        User user = new User("张三",15);
        ObjectMapper mapper = new ObjectMapper();

        //对象转json
        String string = mapper.writeValueAsString(user);
        System.out.println("json字符串" + string);

        //json转对象
        User user1 = mapper.readValue(string, User.class);
        System.out.println("对象" + user1);
    }

在这里插入图片描述

List(String)与json互转

@Test
    public void List_json() throws IOException {
        ArrayList<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        ObjectMapper mapper = new ObjectMapper();

        //list转json
        String string = mapper.writeValueAsString(list);
        System.out.println("json字符串" + string);

        //json转list
        List list1 = mapper.readValue(string, ArrayList.class);
        System.out.println("list为"+list1);
    }

在这里插入图片描述

List (pojo) 与json互转

  @Test
    public void List_pojo() throws IOException {
        ArrayList<User> list = new ArrayList<>();
        list.add(new User("张三",15));
        list.add(new User("李四",16));
        ObjectMapper mapper = new ObjectMapper();

        //list转json
        String string = mapper.writeValueAsString(list);
        System.out.println("json字符串" + string);

        //json转list
        List list1 = mapper.readValue(string, ArrayList.class);
        System.out.println("list为"+list1);
    }

在这里插入图片描述

Map<String,string> 与json互转

@Test
    public void map_json() throws IOException {
        HashMap<String, String> map = new HashMap<>();
        map.put("1","张三");
        map.put("2","李四");
        ObjectMapper mapper = new ObjectMapper();

        //map转json
        String string = mapper.writeValueAsString(map);
        System.out.println("json字符串" + string);

        //json转map
        HashMap hashMap = mapper.readValue(string, HashMap.class);
        System.out.println("map为"+hashMap);
    }

Map<String,User> 与json互转

在这里插入图片描述

 @Test
    public void map_pojo_json() throws IOException {

        HashMap<String, User> map = new HashMap<>();
        map.put("1",new User("张三",15));
        map.put("2",new User("李四",16));
        ObjectMapper mapper = new ObjectMapper();

        //map转json
        String string = mapper.writeValueAsString(map);
        System.out.println("json字符串" + string);

        //json转map
        HashMap hashMap = mapper.readValue(string, HashMap.class);
        System.out.println("map为"+hashMap);
    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值