Example of parsing json using jackson

Source: Internet
Author: User
Tags object serialization


First, download three packages at Github FasterXML. The three core modules are Streaming ("jackson-core") defines low-level streaming API, and includes JSON-specific implementationsAnnotations ("jackson-annotations") contains standard Jackson annotationsDatabind ("jackson-databind") implements data-binding (and object serialization) support on streaming package; it depends both on streaming and annotations packages.
Json file:

{     "name":" vonzhou",     "age":123,     "isMan":true}


POJO class:
package jackson.test;import java.util.HashMap;import java.util.Map;import com.fasterxml.jackson.annotation.JsonProperty;public class ExamplePOJO {      @JsonProperty( "name")      private String name;           @JsonProperty( "age")      private Integer age;           @JsonProperty( "isMan")      private Boolean isMan;           private Map
 
   additionalProperties = new                HashMap
  
   ();           @JsonProperty( "name")      public String getName() {            return name;     }      @JsonProperty( "name")      public void setName(String name) {            this. name = name;     }      @JsonProperty( "age")      public Integer getAge() {            return age;     }      @JsonProperty( "age")      public void setAge(Integer age) {            this. age = age;     }      @JsonProperty( "isMan")      public Boolean getIsMan() {            return isMan;     }      @JsonProperty( "isMan")      public void setIsMan(Boolean isMan) {            this. isMan = isMan;     }      public Map
   
     getAdditionalProperties() {            return additionalProperties;     }      public void setAdditionalProperties(String name, Object obj) {            this. additionalProperties.put(name, obj);     }           @Override      public String toString() {            return "ExamplePOJO{\n" +                      "name :'" + name + "',\n" +                      "age : " + age + ",\n" +                      "isMan :" + isMan + ",\n" +                      "additionalProperties : " + additionalProperties +                      "\n}";     }     }
   
  
 


Json File Parsing class:
package jackson.test;import java.io.File;import java.io.IOException;import com.fasterxml.jackson.core.JsonParseException;import com.fasterxml.jackson.databind.JsonMappingException;import com.fasterxml.jackson.databind.ObjectMapper;public class Driver {     public static void main(String[] args) {          ObjectMapper mapper = new ObjectMapper();          try {               ExamplePOJO bean = mapper.readValue(new File("test.json"),                                                                  ExamplePOJO.class);               System.out.println("name : " + bean.getName());               System.out.println("age : " + bean.getAge() );               System.out.println("isMan : " + bean.getIsMan() );               System.out.println("===================");               System.out.println(bean.toString());          } catch (JsonParseException e) {               e.printStackTrace();          }catch (JsonMappingException e){               e.printStackTrace();          }catch (IOException e){               e.printStackTrace();          }                  }}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.