Using Jackson for JSON parsing and serialization under Java

Source: Internet
Author: User
Tags dateformat

The common JSON libraries under Java are Gson, Json-lib, and Jackson, and Jackson is relatively efficient, using Jackson for JSON and Java object transformations in the project, with some of Jackson's JSON operations described below. First, prepare to go to the official website to download the Jackson Toolkit, Http://wiki.fasterxml.com/JacksonDownload. Jackson has 1.x series and 2.x series, up to now the latest version of the 2.x series is the 2.2.3,2.x series has 3 jar packages to download: Jackson-core-2.2.3.jar (Core jar package,  ) Jackson-annotations-2.2.3.jar (the package provides JSON annotation support,) Jackson-databind-2.2.3.jar () [Java] View plain copy on code    Derive to my Code slice//json serialization and deserialization using the user class import java.util.Date;      public class User {private String name;      Private Integer age;      Private Date birthday;            Private String Email;      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 Date Getbirthday () {return birthday;      } public void Setbirthday (Date birthday) {this.birthday = birthday;     }       Public String Getemail () {return email;      } public void Setemail (String email) {this.email = email;  }} II, Java object to Json[json serialization][java] View plain copy on code to view the snippet derived from my Code slice import java.io.IOException;  Import java.text.ParseException;    Import Java.text.SimpleDateFormat;    Import Com.fasterxml.jackson.databind.ObjectMapper; public class Jacksondemo {public static void main (string[] args) throws ParseException, IOException {User          user = new User ();           User.setname ("Wang");          User.setemail ("[email protected]");                    User.setage (20);          SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd");                           User.setbirthday (Dateformat.parse ("1996-10-01"));          /** * Objectmapper is the core of the JSON operation, and all of Jackson's JSON operations are implemented in Objectmapper.          * Objectmapper has multiple JSON serialization methods that can store JSON strings in different media such as file, OutputStream, and so on. * WriteValue (File arg0, Object arg1) turns arg1 into JSON sequence and saves to aThe rg0 file.          * WriteValue (OutputStream arg0, Object arg1) turns arg1 into a JSON sequence and saves it in the arg0 output stream.          * Writevalueasbytes (Object arg0) turns the arg0 into a JSON sequence and outputs the result into a byte array.          * Writevalueasstring (Object arg0) turns the arg0 into a JSON sequence and outputs the result as a string.                    */Objectmapper Mapper = new Objectmapper (); User class to JSON//output result: {"name": "Wang", "Age": "Birthday": 844099200000, "email": "[email protected]"} stri          ng JSON = mapper.writevalueasstring (user);                    SYSTEM.OUT.PRINTLN (JSON); Java collection to JSON//output result: [{"Name": "Wang", "Age": "Birthday": 844099200000, "email": "[email protected]"}] L          ist<user> users = new arraylist<user> ();          Users.add (user);          String jsonlist = mapper.writevalueasstring (users);      System.out.println (jsonlist);  }} III, JSON to Java class [JSON deserialization][java] View plain copy on code to see the snippet derived from my Code slice import java.io.IOException;  Import java.text.ParseException; Import Com.fasterxml.jackson.daTabind.    Objectmapper; public class Jacksondemo {public static void main (string[] args) throws ParseException, IOException {Strin                    G json = "{\" name\ ": \" Wang \ ", \" age\ ": 20,\" birthday\ ": 844099200000,\" email\ ": \" [email protected]\ "}";          /** * Objectmapper supports JSON deserialization of data from byte[], File, InputStream, String, and so on.          */Objectmapper Mapper = new Objectmapper ();          User user = Mapper.readvalue (JSON, user.class);      SYSTEM.OUT.PRINTLN (user); }} Iv. JSON annotations Jackson provides a series of annotations to facilitate the control of JSON serialization and deserialization, and some common annotations are described below. @JsonIgnore This annotation is used on attributes, which is ignored when the JSON operation is performed. @JsonFormat This annotation is used on attributes to convert the date type directly to the desired format, such as @jsonformat (pattern = "Yyyy-mm-dd hh-mm-ss"). @JsonProperty This annotation is used on attributes to serialize the name of the property to another name, such as serializing the Truename property to name, @JsonProperty ("name").  [Java] View plain copy on code to see a snippet derived from my Code slice import java.util.Date;    Import com.fasterxml.jackson.annotation.*;            public class User {private String name; No JSON serialization age attribute @JsonIgnore private IntegER age;            Format Date attribute @JsonFormat (pattern = "yyyy mm DD Day") Private date birthday;            The serialized email attribute is mail @JsonProperty ("Mail") private String email;      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 Date Getbirthday () {return birthday;      } public void Setbirthday (Date birthday) {this.birthday = birthday;      } public String Getemail () {return email;      } public void Setemail (String email) {this.email = email;  }} import java.io.IOException;  Import java.text.ParseException;    Import Java.text.SimpleDateFormat;    Import Com.fasterxml.jackson.databind.ObjectMapper; public class Jacksondemo {public static void main (string[] Args) throws ParseException, IOException {User user = new User ();           User.setname ("Wang");          User.setemail ("[email protected]");                    User.setage (20);          SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd");                           User.setbirthday (Dateformat.parse ("1996-10-01"));          Objectmapper mapper = new Objectmapper ();          String JSON = mapper.writevalueasstring (user);          SYSTEM.OUT.PRINTLN (JSON);   Output: {"name": "Wang", "Birthday": "September 30, 1996", "Mail": "[Email protected]"}}}

  

Using Jackson for JSON parsing and serialization under Java

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.