码迷,mamicode.com
首页 > 编程语言 > 详细

Java:JSON解析利器JackSon

时间:2018-02-04 16:37:52      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:ann   core   eva   repr   main   private   first   html   pre   

Java:JSON解析利器JackSon

JackSon基础

1.Maven项目引入

<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>

2.使用方式

public class ReadWriteJackson {
  public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
   
    String jsonInput = "{\"id\":0,\"firstName\":\"Robin\",\"lastName\":\"Wilson\"}";
    Person q = mapper.readValue(jsonInput, Person.class);
    System.out.println("Read and parsed Person from JSON: " + q);
   
    Person p = new Person("Roger", "Rabbit");
    System.out.print("Person object " + p + " as JSON = ");
    mapper.writeValue(System.out, p);
  }
}

3.使用注解忽略未知字段

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Stuff {
    private String stuff_name;
    private Integer stuff_number;
    private Double stuff_price;
    private String stuff_bid_id;
    . . . . . .    
}

  

 

Java:JSON解析利器JackSon

标签:ann   core   eva   repr   main   private   first   html   pre   

原文地址:https://www.cnblogs.com/MrSaver/p/8413332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!