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