zoukankan      html  css  js  c++  java
  • 如何将java对象转换成json数据



    package cn.hopetesting.com.test;

    import cn.hopetesting.com.domain.User;
    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.io.IOException;


    /**
    * @author newcityman
    * @date 2019/9/17 - 23:12
    步骤1、导入jackson的三个包
    2、创建一个user对象,并赋值
    3、创建jackson的核心对象ObjectMapper
    4、调用ObjectMapper的writevalue等相关方法
    */

    public class TestJson {
    @Test
    public void test1() throws IOException {
    //1、创建对象
    User user = new User("zmy",20,"male");
    //2、创建jackson的核心对象 ObjectMapper
    ObjectMapper mapper = new ObjectMapper();
    //3、转换
    /*try {
    String json = mapper.writeValueAsString(user);
    System.out.println(json);
    } catch (JsonProcessingException e) {
    e.printStackTrace();
    }*/
    //将数据写到d://a.txt文件中
    // mapper.writeValue(new File("d://a.txt"),user);
    //将数据关联到writer中
    mapper.writeValue(new FileWriter("d://b.txt"),user);

    }

    @Test
    public void test2() throws IOException {
    //1、创建对象
    User user = new User("zmy",20,"male");
    user.setBirthday(new Date());
    //2、创建jackson的核心对象 ObjectMapper
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(user);
    System.out.println(json);

    }

    @Test
    public void test3() throws IOException {
    //1、创建user和list对象
    User user = new User("zmy",20,"male",new Date());
    User user1 = new User("light",10,"male",new Date());
    User user2 = new User("cityboy",5,"male",new Date());

    List<User> ps = new ArrayList<User>();
    ps.add(user);
    ps.add(user1);
    ps.add(user2);
    //2、创建jackson的核心对象 ObjectMapper
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(ps);
    System.out.println(json);

    }

    @Test
    public void test4() throws IOException {
    Map<String,Object> map = new HashMap<String, Object>();
    map.put("name","zmy");
    map.put("age",15);
    map.put("gender","男");

    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(map);
    System.out.println(json);
    }

    //演示 json字符串转换为java对象
    @Test
    public void test5() throws IOException {
    //初始化json字符串
    String json="{"gender":"","name":"zmy","age":15}";
    //创建ObjectMapper对象
    ObjectMapper mapper = new ObjectMapper();
    //转换json字符串为java对象
    User user = mapper.readValue(json, User.class);
    System.out.println(user);
    }
    }
    
    
  • 相关阅读:
    函数和函数模版在一个。cpp中的情况!(除了左移和右移,其他的不要用友元函数!!!)
    const typedef 和指针的问题(这里必须初始化的才初始化了,不必须的则没有初始化)
    const constptr 和引用的盲点(未解决)
    对于数据流建模和行为级建模的梳理(重点)
    vivado实现模16的计数器
    用vivado实现4比特加法器
    三输入或门(发现这个玩意很不好耍,编程出现错误,不知道哪里出现的,一不小心2输成3也无法查证)
    SpringMVC第一个例子
    Mybatis与Spring的mapper代理整合方法
    Mybatis与Spring的原生Dao整合
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11538020.html
Copyright © 2011-2022 走看看