Java Jackson读取Json以及使用Bean生成Json

一.环境配置

JavaSE版本:1.8
Maven配置如下:


    UTF-8
    2.8.1



    
      junit
      junit
      3.8.1
      test
   
   
   
      com.fasterxml.jackson.core
      jackson-core
      ${jackson-version}
   
   
      com.fasterxml.jackson.core
      jackson-databind
      ${jackson-version}
   
   
      com.fasterxml.jackson.core
      jackson-annotations
      ${jackson-version}
   

解析JSON

要解析的JSON数据如下:

{
    name:"韩超",
    age:18,
    code:1507030123,
    hobbies:["football","game","baskerball"]
    details:
    {
        birthday:869760000,
        extar:[1321,"hahaha","zzz421"]
    }
}

解析示例代码

@Test
public void testParseJson() {
    //原数据
    String data="{" + 
            "   \"name\":\"韩超\"," + 
            "   \"age\":18," + 
            "   \"code\":1507030123," + 
            "   \"hobbies\":[\"football\",\"game\",\"baskerball\"]," + 
            "   \"details\":" + 
            "   {" + 
            "       \"birthday\":869760000," + 
            "       \"extar\":[1321,\"hahaha\",\"zzz421\"]" + 
            "   }" + 
            "}";
    //创建一个ObjectMapper对象
    ObjectMapper objectMapper=new ObjectMapper();
    //读取json数据,返回一个JsonNode对象指向根节点
    try {
        JsonNode jsonNode=objectMapper.readTree(data);
        //利用JsonNode的path方法获取子节点,path方法的返回值也是JsonNode
        JsonNode subNode=jsonNode.path("details");//得到details节点
        //JsonNode对象的asInt(),asText()等方法用于获取值
        
        System.out.println(jsonNode.path("name").asText());//返回字符串,韩超
        System.out.println(jsonNode.path("age").asInt());//返回整形数据,18
        System.out.println(jsonNode.path("code").asLong());//返回长整型数据,1507030123
        
        //对于数组,可以使用size方法获取数组长度,get(index)获取索取为index的值
        for(int i=0;i

JavaBean转换成JSON

Bean示例

public class MyBean implements Serializable{
    private String name;
    private Integer age;
    private Long code;
    private String[] hobbies;
    private Map details;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Long getCode() {
        return code;
    }
    public void setCode(Long code) {
        this.code = code;
    }
    public String[] getHobbies() {
        return hobbies;
    }
    public void setHobbies(String[] hobbies) {
        this.hobbies = hobbies;
    }
    public Map getDetails() {
        return details;
    }
    public void setDetails(Map details) {
        this.details = details;
    }
}

生成JSON代码示例

@Test
public void testJsonBuilder() {
    //写入数据
    MyBean myBean=new MyBean();
    myBean.setAge(18);
    myBean.setName("韩超");
    String[] hobbies= {"game","it"};
    Object[] extra= {"1212",233,"haha","hdfa31"};
    Map map=new HashMap();
    map.put("birthday",869760000);
    map.put("extra", extra);
    myBean.setDetails(map);
    
    ObjectMapper objectMapper=new ObjectMapper();
    try {
        //利用writeValueAsString方法将一个Bean转换成Json数据
        System.out.println(objectMapper.writeValueAsString(myBean));
        //如果要生成json数组,只需使用List对象存储一个个Bean就行了
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
}

输出结果

你可能感兴趣的:(Java Jackson读取Json以及使用Bean生成Json)