Using Jackson to parse the JSON demo sample

Source: Internet
Author: User
Tags object serialization


First you need to download 3 packages, on GitHub Fasterxml, these three core modules are:Streaming ("jackson-core") defines low-level streaming API, and includes json-specific implementations Annotations ("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:
<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >{     "name": "Vonzhou",     "age": 123,     "Isman":true}</span>


Pojo class:
<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >package Jackson.test;import Java.util.hashmap;import Java.util.map;import Com.fasterxml.jackson.annotation.jsonproperty;public class Examplepojo {@JsonProperty ("name") Private String N           Ame           @JsonProperty ("age") private Integer age;           @JsonProperty ("Isman") private Boolean Isman;           Private map<string,object> additionalproperties = new hashmap<string, object> ();     @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.     } @JsonProperty ("Isman") public Boolean Getisman () {return isman; } @JsonProperty ("Isman")      public void Setisman (Boolean isman) {this. Isman = Isman;     } public map<string, Object> 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}"; }}</span>


the class that parses the JSON file:
<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >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) {Ob          Jectmapper 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 (); }}}</span>


Using Jackson to parse the JSON demo sample

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.