Jackson JSON Processor

Source: Internet
Author: User

Jackson provides interfaces that can be converted between JSON and beans

1. An example

 Public classJsontojavabean { Public Static voidMain (string[] args) {String str= "{\" student\ ": [{\" name\ ": \" leilei\ ", \" age\ ": 23},{\" name\ ": \" leilei02\ ", \" age\ ": 23}]}"; Student Stu=NULL; List<Student> list =NULL; Try{objectmapper Objectmapper=NewObjectmapper (); Studentlist studentlist=objectmapper.readvalue (str, studentlist.class); List=studentlist.getstudent (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }          for(Student s:list) {System.out.println (S.getname ()+"   "+s.getage ()); }    }}

2. A second example

 Public Static voidMain (string[] args) {ArrayList<Student> list=NewArraylist<student>(); Student S1=NewStudent (); S1.setname ("Leilei"); S1.setage (23); Student S2=NewStudent (); S2.setname ("Leilei02"); S2.setage (23);        List.add (S1);                 List.add (S2); StringWriter Str=NewStringWriter (); Objectmapper Objectmapper=NewObjectmapper (); Try{objectmapper.writevalue (str, list); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace ();    } System.out.println (str); }

The usage I see

 Public throws jsonparseexception, Jsonmappingexception, IOException {        returnnew objectmapper (). ReadValue (JSON, Session. ) Class);    }

Summarize

1. JSON exists as a string in Java

2. The information required for the conversion is the original JSON data and the definition of the transformation standard bean

3. You can convert an array and a single object

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.