1 Public Static voidJsonobjecttojavabean ()throwsclasscastexception{2Jsonobject Jsonobject =NewJsonobject ();3Jsonobject.put ("id", 1);4Jsonobject.put ("name", "Yang Wenpei");5Jsonobject.put ("Sex", "male");6Jsonobject.put ("Age",NULL);//if a null string should be represented as "' null '; int null defaults to 07Jsonobject.put ("Birthday", "1993-01-23");8list<string> list =NewArraylist<string>();9List.add ("Basketball");TenList.add ("Table tennis"); OneList.add ("Badminton"); AJsonobject.put ("Hobby", list); -Jsonobject.put ("Boo",false); -System.out.println ("jsonobject.tostring () =" +jsonobject.tostring ()); the Try{ -Student Student = (Student) Jsonobject.tobean (jsonobject,student.class); -System.out.println ("student=" +student); -}Catch(ClassCastException exception) { +System.err.println ("exception=" +exception.getmessage ()); - } +}
Map Conversions:
1 Public Static voidMaptojsonobject () {2map<string, object> map =NewHashmap<string,object>();3Map.put ("id", 1);4Map.put ("name", "Yang Wenpei");5Map.put ("Sex",NULL);6Map.put ("Age", 23);7Map.put ("Birthday", "1993-01-23");8list<string> list =NewArraylist<string>();9List.add ("Basketball");TenList.add ("Table tennis"); OneList.add ("Badminton"); AMap.put ("Hobby", list); -Map.put ("Boo",false); -Jsonobject Jsonobject =jsonobject.fromobject (map); theSystem.out.println ("map.tostring () =" +map.tostring ()); -System.out.println ("jsonobject.tostring () =" +jsonobject.tostring ()); - Try{ -Student Student = (Student) Jsonobject.tobean (jsonobject,student.class); +System.out.println ("student=" +student); -}Catch(ClassCastException exception) { +System.err.println ("exception=" +exception.getmessage ()); A } at}
Convert JSON to JavaBean