The transformation between JSON and JavaBean

Source: Internet
Author: User

Then the previous http://www.cnblogs.com/ya-qiang/p/9009134.html essay continues to introduce the transformation between JSON and Java objects.

One, Java common Object and JSON string of the mutual transfer

Java Object---->json

First create a Java object:

public class Student {//name private string name;//age private String age;//address private string Address;public string GetName () {R Eturn name;} public void SetName (String name) {this.name = name;} Public String Getage () {return age;} public void Setage (String age) {this.age = age;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;} @Overridepublic String toString () {return "Student [name=" + name + ", age=" + Age + ", address=" + address + "]";}}

Now the Java object is converted to JSON form:

public static void Convertobject () {Student stu=new Student (); Stu.setname ("JSON"); Stu.setage ("All"); Stu.setaddress (" Beijing Xicheng District ");//1, using jsonobjectjsonobject json = Jsonobject.fromobject (stu);//2, using Jsonarrayjsonarray array= Jsonarray.fromobject (Stu); String strjson=json.tostring (); String strarray=array.tostring (); System.out.println ("Strjson:" +strjson); System.out.println ("Strarray:" +strarray);}

A student entity class is defined, and then converted to a JSON string using Jsonobject and Jsonarray, respectively, with the result of printing:

Json-->javabean

It shows how to convert a Java object into a JSON string and see how to convert the JSON string format to a Java object.

You first need to define two different formats of strings that need to be escaped with \ pairs of double quotes.

public static void Jsonstrtojava () {//defines two different format strings string objectstr= "{\" name\ ": \" json\ ", \" age\ ": \" 24\ ", \" address\ ": \ "Beijing Xicheng District \"} "; String arraystr= "[{\" name\ ": \" json\ ", \" age\ ": \" 24\ ", \" address\ ": \" Xicheng District of Beijing \ "}]";//1, using Jsonobjectjsonobject Jsonobject=jsonobject.fromobject (OBJECTSTR); Student stu= (Student) Jsonobject.tobean (Jsonobject, student.class);//2, using Jsonarrayjsonarray jsonarray= Jsonarray.fromobject (ARRAYSTR);//Get the first element of Jsonarray object O=jsonarray.get (0); Jsonobject Jsonobject2=jsonobject.fromobject (o); Student stu2= (Student) Jsonobject.tobean (JsonObject2, Student.class); System.out.println ("Stu:" +stu); System.out.println ("STU2:" +STU2);}

Operation Result:

As you can see from the code above, using Jsonobject can easily convert a JSON-formatted string into a Java object, but using Jsonarray is not as easy as it has a "[]" symbol, so after we get the Jsonarray object here, Take its first element, which is a student transformation we need, and then use Jsonobject to easily get it.

Second, the list and JSON string of the mutual transfer

The following converts the list to a JSON string:

public static void Convertlistobject () {Student stu=new Student (); Stu.setname ("JSON"); Stu.setage ("23"); Stu.setaddress ("Xicheng District, Beijing"); Student stu2=new Student () stu2.setname ("JSON2"); Stu2.setage ("at") stu2.setaddress ("Xicheng District, Beijing"); //Note If the list is multiple objects than you need to use Jsonarray Jsonarray Array=jsonarray.fromobject (STU); String strarray=array.tostring (); System.out.println ("Strarray:" +strarray);}

The result is: strarray:[{"address": "Xicheng District, Beijing", "name": "JSON", "Age": "},{", "Address": "Xicheng District, Beijing", "name": "JSON2", "Age": "23"}]

If you use Jsonobject to convert it will appear: Exception in Thread "main" net.sf.json.JSONException: ' object ' was an array. Use Jsonarray instead

The following converts the JSON string to a list

public static void Jsontolist () {String arraystr= "[{\" name\ ": \" json\ ", \" age\ ": \" 24\ ", \" address\ ": \" Beijing Xicheng District \ "},{\" Name\ ": \" json2\ ", \" age\ ": \" 24\ ", \" address\ ": \" Xicheng District, Beijing, "}]";//Convert to listlist<student> list2= (list<student >) jsonarray.tolist (Jsonarray.fromobject (ARRAYSTR), student.class); for (Student stu:list2) {System.out.println ( Stu);} Convert to array student[] SS = (student[]) Jsonarray.toarray (Jsonarray.fromobject (ARRAYSTR), student.class); for (Student STUDENT:SS) {System.out.println (student);}}

The result of the operation is:

Third, map and JSON string cross-transfer

Map converted to JSON string

public static void Maptojson () {Student stu=new Student (); Stu.setname ("JSON"); Stu.setage ("All"); Stu.setaddress ("Shanghai, China" ); Map<string,student> map=new hashmap<string,student> () map.put ("First", Stu);//1, Jsonobjectjsonobject Mapobject=jsonobject.fromobject (map); System.out.println ("MapObject:" +mapobject.tostring ());//2, Jsonarrayjsonarray maparray=jsonarray.fromobject (map) ; System.out.println ("Maparray:" +maparray.tostring ());}

Operation Result:

Convert JSON string to map:

public static void Jsontomap () {String strobject= "{\" first\ ": {\" address\ ": \" Shanghai, China \ ", \" age\ ": \" 23\ ", \" name\ ": \" json\ " }} ";//jsonobjectjsonobject jsonobject=jsonobject.fromobject (Strobject); Map map=new HashMap () map.put ("First", student.class);//Tobean method required, three parameters Mybean my= (mybean) Jsonobject.tobean ( Jsonobject, Mybean.class, map); System.out.println (My.getfirst ());}

Note that when you convert to map, you need to create a class that needs to have the first attribute in the JSON string:

Using the Tobean () method is passed three parameters, the first is the Jsonobject object, the second is Mybean.class, and the third is a map object. The Mybean can be used to know that there is a first attribute in this class, and its type is student, which corresponds to the key and value type in map, that is, first corresponds to the type of the key first type corresponding to the value.

Operation Result:

The transformation between JSON and JavaBean

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.