A string string of JSON and a list object in Java are converted to each other

Source: Internet
Author: User
Tags convert json to string convert string to json string to json

on the front end:1if JSON is converted by a list object, you can directly traverse the JSON to read the data. 2If you need to convert the front-end list object to JSON to the background, param is the AJAX parameter, then the conversion is as follows: Var jsonstr=json.stringify (list); var param={}; Param.jsonstr=Jsonstr;
In the background:1. Convert string to list (str to list) List<T> list =NewArraylist<t>(); Jsonarray Jsonarray= Jsonarray.fromobject (str);//convert string to JSONList = Jsonarray.tolist (jsonarray,t);//the T here is class<t>.in this case, because the JSON string is obtained using Jsonarray.fromobject (collenction), all, when the JSON string is converted to a JSON object, You can only use jsonarray.tocollection, and be sure to take the following class parameters.
2. Convert list to Jsonjsonarray JSON=Jsonarray.fromobject (object); String Str= Json.tostring ();//convert JSON to stringagain: In the teacher class, there are three properties. PrivateString Teaid; PrivateString Teaname; PrivateList<student>Stus; Teacher teacher_1=NewTeacher ("No. 1", "Teacher 1", Stus); Jsonobject obj=Jsonobject.fromobject (teacher_1); This step, when the teacher object is converted to a JSON string, there is no problem. Below, the jsonobject is converted to a teacher object, and if you do not add the following class parameter, the error will be reported here. After the addition, this step is normal. Teacher Teacherbean= (Teacher) jsonobject.tobean (obj, Teacher.class); Student Studentbean= Teacherbean.getstus (). Get (0); When you remove the value of the Stus property from the Teacherbean object, you are prompted to: Java.lang.ClassCastException:net.sf.ezmorph.bean.MorphDynaBean cannot is cast to Com.edu.xukai.Student can be solved in this way: Map<string, class> classmap =NewHashmap<string, class>(); Classmap.put ("Stus", Student.class); Teacher Teacherbean= (Teacher) jsonobject.tobean (obj, Teacher.class, Classmap); Where the Map object is the type of each property in the Teacherbean object, the map amount key is the property each time, and value is the type of the property. Jsonarray.toarray (Jsonarray, ObjectClass, Classmap) jsonarray.tolist (Jsonarray, ObjectClass, Classmap) also support such a way.

A string string of JSON and a list object in Java are converted to each other

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.