Java object and JSON Data transformation implementation 3-using the Jackson implementation

Source: Internet
Author: User

Test code:

Package Com.yanek.util.json;import Java.io.ioexception;import Java.io.stringwriter;import java.util.ArrayList; Import Java.util.list;import Com.fasterxml.jackson.core.jsonfactory;import Com.fasterxml.jackson.core.jsongenerator;import Com.fasterxml.jackson.core.jsonparseexception;import Com.fasterxml.jackson.databind.jsonmappingexception;import Com.fasterxml.jackson.databind.objectmapper;public Class Jacksonutil {/** * @param args */public static void main (string[] args) {Hotel h1=new Hotel (); H1.setid (1); h1.setname ("name1"); Hotel H2=new (); H2.setid (2); H2.setname ("name2"); Roomtypevo t1=new Roomtypevo () t1.setprice ("a") T1.settypeid (1); T1.settypename ("nnn1"); List<roomtypevo> roomtypevos=new arraylist<roomtypevo> (); Roomtypevos.add (t1); Roomtypevos.add (t1); H1.setroomtypevos (Roomtypevos); Hotel=new Hotel (); try {System.out.println ("Json1:::" +bean2json (H1)); String S=bean2json (H1); Hotel=json2bean (S,hotel.class); System.out.println ("Json2:::" +bean2json (hotel)); System.out.prinTLN ("Json3:::" +bean2json (Roomtypevos)); String S2=bean2json (Roomtypevos); List<roomtypevo> roomtypevos2=new arraylist<roomtypevo> (); Roomtypevos2=json2bean (S2,list.class); System.out.println ("Json4:::" +bean2json (ROOMTYPEVOS2));} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}        public static String Bean2json (Object obj) throws IOException {Objectmapper mapper = new Objectmapper ();        StringWriter SW = new StringWriter ();        Jsongenerator Gen = new Jsonfactory (). Createjsongenerator (SW);        Mapper.writevalue (gen, obj);        Gen.close ();    return sw.tostring (); } public static <T> T Json2bean (String jsonstr, Class<t> objclass) throws Jsonparseexception, Js        Onmappingexception, IOException {objectmapper mapper = new Objectmapper ();    Return Mapper.readvalue (Jsonstr, objclass); }}

The output is as follows:

json1:::{"id": 1, "name": "Name1", "Roomtypevos": [{"typeID": 1, "TypeName": "Nnn1", "Price": "},{", "typeID": 1, " TypeName ":" Nnn1 "," Price ":"}]}json2:::{"," id ": 1," name ":" Name1 "," Roomtypevos ": [{" typeID ": 1," TypeName ":" Nnn1 "," Price ":"},{"typeID": 1, "TypeName": "Nnn1", "Price": "}]}json3:::[{", "typeID": 1, "TypeName": "Nnn1", "Price": "20"} , {"typeID": 1, "TypeName": "Nnn1", "Price": "}]json4:::[{", "typeID": 1, "TypeName": "Nnn1", "Price": ""},{"," typeID ": 1, "TypeName": "Nnn1", "Price": "20"}]

Related entity classes:


Package Com.yanek.util.json;import Java.util.list;public class Hotel {public list<roomtypevo> Getroomtypevos () { return Roomtypevos;} public void Setroomtypevos (list<roomtypevo> roomtypevos) {roomtypevos = Roomtypevos;} Private list<roomtypevo> roomtypevos;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} private int id;private String name;} Package Com.yanek.util.json;public class Roomtypevo {public int gettypeid () {return typeid;} public void Settypeid (int typeid) {This.typeid = typeID;} Public String Gettypename () {return typename;} public void Settypename (String typename) {this.typename = typename;} Public String GetPrice () {return price;} public void Setprice (String price) {this.price = Price;} private int typeid;private string typename;private string price;}


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.