Data binding _java in the Jackson Library of Java with instance parsing

Source: Internet
Author: User

The data binding APIs are used for JSON conversions and for using attribute access or annotation Pojo (normal Java objects). Here are two of its types.

Simple data binding-converts JSON from Java Maps, Lists, Strings, Numbers, Booleans, and null objects.

Full data binding-converts JSON to any Java type. We will be bound separately in the next chapter.

Objectmapper read/write JSON two kinds of data binding. Data binding is the most convenient way to be a JAXB parser similar to XML.

Simple Data binding
simple data binding means that JSON maps to the Java core data type. The following table lists the relationships between the JSON type and the Java type.

Let's take a look at simple data manipulation bindings. Here, we will map Java basic Type Direct JSON, and vice versa.

Create a name for Jacksontester in the Java class file in the directory c:\>jackson_workspace.

File:JacksonTester.java

Import Java.io.File;
Import java.io.IOException;
Import Java.util.HashMap;

Import Java.util.Map;
Import org.codehaus.jackson.JsonGenerationException;
Import org.codehaus.jackson.JsonParseException;
Import org.codehaus.jackson.map.JsonMappingException;

Import Org.codehaus.jackson.map.ObjectMapper;
     public class Jacksontester {public static void main (String args[]) {Jacksontester tester = new Jacksontester ();

      try {objectmapper mapper = new Objectmapper (); 
      map<string,object> Studentdatamap = new hashmap<string,object> ();

      Int[] marks = {1,2,3};
      Student Student = new Student ();
      Student.setage (10);
      Student.setname ("Mahesh");
      JAVA Object studentdatamap.put ("Student", student);   
      JAVA String studentdatamap.put ("name", "Mahesh Kumar");
      JAVA Boolean studentdatamap.put ("verified", boolean.false);

      Array studentdatamap.put ("Marks", marks); Mapper.writevalue (The New File ("Student.json "), Studentdatamap); Result Student.json//{//"student": {"name": "Mahesh", "Age": ten},//"Marks": [1,2,3],//"Verifie D ": false,//" name ":" Mahesh Kumar "//} Studentdatamap = Mapper.readvalue (New File (" Student.json "), Map.

      Class);
      System.out.println (Studentdatamap.get ("student"));
      System.out.println (Studentdatamap.get ("name"));
      System.out.println (Studentdatamap.get ("verified"));
   System.out.println (Studentdatamap.get ("Marks"));
   catch (Jsonparseexception e) {e.printstacktrace ();
   catch (Jsonmappingexception e) {e.printstacktrace ();
   catch (IOException e) {e.printstacktrace ();
  }} class Student {private String name;
  private int age;
  Public Student () {} public String GetName () {return name;
  public void SetName (String name) {this.name = name;
  public int getage () {return age;
  public void Setage (int age) {this.age = age; } pUblic String toString () {return ' Student [name: ' +name+ ', Age: ' + age+ '] ';

 } 
}

Validation results

Use Javac to compile the following classes:

Copy Code code as follows:

C:\jackson_workspace>javac Jacksontester.java

Now run Jacksontester See the results:
Copy Code code as follows:

C:\jackson_workspace>java Jacksontester

Verifying output results

{Name=mahesh, age=10}
Mahesh Kumar
False
[1, 2, 3]

Full data binding
full data binding means that JSON is mapped to any Java object.

Create an Objectmapper instance
objectmapper mapper = new Objectmapper (); 
Map JSON content to Student object
Student Student = Mapper.readvalue (New File ("Student.json"), Student.class); 
   //map Student object to JSON content
Mapper.writevalue (new File ("Student.json"), Student);

Let's take a look at simple data manipulation bindings. Here, we'll map Java objects directly to JSON, and vice versa.

Create a c:\>jackson_workspace named Jacksontester in the Java class file directory.

File:JacksonTester.java

Import Java.io.File;

Import java.io.IOException;
Import org.codehaus.jackson.JsonGenerationException;
Import org.codehaus.jackson.JsonParseException;
Import org.codehaus.jackson.map.JsonMappingException;

Import Org.codehaus.jackson.map.ObjectMapper;
   public class Jacksontester {public static void main (String args[]) {Jacksontester tester = new Jacksontester ();
     try {Student Student = new Student ();
     Student.setage (10);
     Student.setname ("Mahesh");

     Tester.writejson (student);
     Student student1 = Tester.readjson ();

   System.out.println (STUDENT1);
   catch (Jsonparseexception e) {e.printstacktrace ();
   catch (Jsonmappingexception e) {e.printstacktrace ();
   catch (IOException e) {e.printstacktrace (); } private void Writejson (Student Student) throws Jsongenerationexception, Jsonmappingexception, ioexception{OBJ 
   Ectmapper mapper = new Objectmapper ();
  Mapper.writevalue (New File ("Student.json"), student); } priVate Student Readjson () throws Jsonparseexception, Jsonmappingexception, ioexception{objectmapper mapper = new OBJECTM
   Apper ();
   Student Student = Mapper.readvalue (New File ("Student.json"), Student.class);
  return student;
  } class Student {private String name;
  private int age;
  Public Student () {} public String GetName () {return name;
  public void SetName (String name) {this.name = name;
  public int getage () {return age;
  public void Setage (int age) {this.age = age;
  Public String toString () {return ' Student [name: ' +name+ ', Age: ' + age+ '] ';

 } 
}

Validation results

Use Javac to compile the following classes:

Copy Code code as follows:

C:\jackson_workspace>javac Jacksontester.java

Now run Jacksontester See the results:
Copy Code code as follows:

C:\jackson_workspace>java Jacksontester

Validating output
Copy Code code as follows:

Student [Name:mahesh, Age:10]

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.