Json-lib for Java and JSON string conversion two

Source: Internet
Author: User

Second, the list and JSON string of the mutual transfer

list--"JSON string

 Public Static voidListtojson () {Student stu=NewStudent (); Stu.setname ("JSON"); Stu.setage ("23"); Stu.setaddress ("Haidian District, Beijing"); List<Student> lists=NewArraylist<student>(); Lists.add (Stu);//1. Using Jsonobject//jsonobject listobject=jsonobject.fromobject (lists);//2. Using JsonarrayJsonarray listarray=jsonarray.fromobject (lists);//System.out.println ("ListObject:" +listobject.tostring ());System.out.println ("Listarray:" +listarray.tostring ());}

I've jsonobject the way I used it, and we'll look at the results before the comments,

Exception in thread "main" net.sf.json.JSONException: ' object ' was an array. Use Jsonarray instead
Tell me that there is an exception, by looking at the source code discovery, when using the Fromobject method will be the parameter type of judgment, here tells us that the passed parameter is an array type, because the use of the ArrayList, then see, the result after the comment,

listarray:[{"Address": "Haidian District, Beijing", "age": "All", "name": "JSON"}]
So the result is normal.

JSON string-"list"

From the above example, we can see that the list object can only be converted to the format of the array object, then we look at the following string to list conversion,

 Public Static voidjsontolist () {String arraystr= "[{\" name\ ": \" json\ ", \" 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 Arraysstudent[] ss = (student[]) Jsonarray.toarray (Jsonarray.fromobject (ARRAYSTR), Student.class); for(Student student:ss) {System.out.println (Student);}}

Printing results,

Student [Name=json, age=24, address=, Xicheng District, Beijing]

Student [Name=json, age=24, address=, Xicheng District, Beijing]

Because the format of the string is a format with "[]", so here jsonarray this object, it has a toarray, ToList method is available, the former is converted to an array in Java, or into a list in Java, because there are entity classes to correspond, Therefore, the type of the generic (Student.class) is specified when it is used, so that the converted object can be obtained.

Third, map and JSON string cross-transfer

map--"JSON string

 Public Static voidMaptojson () {Student stu=NewStudent (); Stu.setname ("JSON"); Stu.setage ("23"); Stu.setaddress ("Shanghai, China"); Map<String,Student> map=NewHashmap<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 ());}

Printing results,

mapobject{"First": {"Address": "Shanghai, China", "age": "+", "name": "JSON"}}
maparray:[{"First": {"Address": "Shanghai, China", "age": "+", "name": "JSON"}]

There are two forms printed on it.

JSON string-"map"

The JSON string cannot be converted directly to a map object, and it takes another way to get the value corresponding to the key in the map.

 Public Static void Jsontomap () {String Strobject= "{\" first\ ": {\" address\ ": \" Shanghai, China \ ", \" age\ ": \" 23\ ", \" name\ ": \" json\ "}}"  ; // Jsonobject Jsonobject jsonobject=jsonobject.fromobject (strobject); Map Map=new  HashMap (); Map.put ("First", Student.  Class); //  Mybean my= (Mybean) Jsonobject.tobean (Jsonobject, Mybean.  Class, map); System.out.println (My.getfirst ());}

Printing results,

Student [Name=json, age=23, address=, Shanghai, China]

Here is the code for Mybean,

 Package Com.cn.study.day4; Import Java.util.Map; Import com.cn.study.day3.Student;  Public class Mybean {private  Student first;  Public Student GetFirst () {return first ;}  Public void Setfirst (Student first) {this. First = First ;}}

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.

The above is a small series to introduce the JSON string and the Java object to convert the example of each other, we hope to help!

Original link: http://www.cnblogs.com/teach/archive/2016/08/20/5791029.html

Json-lib for Java and JSON string conversion two

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.