// Case 1: Convert Java to a JSON string
@ Test
Public voidFromobject (){
// Convert a Java object to a JSON string
Teacher =NewTeacher ();
Teacher. setname ("AA ");
Teacher. setsex ("male ");
Teacher. setage (28 );
Teacher. setbirth (NewDate ());
// JSON string
// {Name: "AA", sex: "male", age: "28", birth :"...."}
// You need to introduce the class library file
System.Out. Println (jsonobject.Fromobject(Teacher ));
}
{"Age": 28, "birth": {"date": 28, "day": 2, "hours": 9, "Minutes": 32, "month ": 1, "seconds": 5, "Time": 1330392725328, "timezoneoffset":-480, "year": 112}, "name": "AA", "sex ": "male "}
// Convert the Java set into a JSON string
@ Test
Public voidFromvector (){
List <teacher> teas =NewArraylist <teacher> ();
For(IntI = 0; I <3; I ++ ){
Teacher =NewTeacher ();
Teacher. setname ("AA ");
Teacher. setsex ("male ");
Teacher. setage (28 );
Teacher. setbirth (NewDate ());
Teas. Add (teacher );
}
// Teas set
System.Out. Println (jsonarray.Fromobject(TEAs). tostring ());
}
[{"Age": 28, "birth": {"date": 28, "day": 2, "hours": 9, "Minutes": 29, "month": 1, "seconds": 18, "Time": 1330392558562, "timezoneoffset":-480, "year": 112}, "name": "AA ", "Sex": "male" },{ "Age": 28, "birth": {"date": 28, "day": 2, "hours": 9, "Minutes": 29, "month": 1, "seconds": 18, "Time": 1330392558562, "timezoneoffset":-480, "year": 112 }, "name": "AA", "sex": "male" },{ "Age": 28, "birth": {"date": 28, "day ": 2, "hours": 9, "Minutes": 29, "month": 1, "seconds": 18, "Time": 1330392558562, "timezoneoffset":-480, "year": 112}, "name": "AA", "sex": "male"}]
// Convert a JSON string to an object
@ Test
Public voidFromjsonobject (){
// Step 1: define it as a string
String teaobj = "{\" Age \ ": 28, \" Birth \ ": {\" date \ ": 28, \" day \ ": 2, \ "hours \": 9, \ "Minutes \": 20, \ "month \": 1, \ "seconds \": 25, \ "time \": 1330392025906, \ "timezoneoffset \":-480, \ "Year \": 112}, \ "Name \": \ "AA \", \ "Sex \": \ "male \"}";
// Step 2: Use the jsonobject. fromobject method to convert its string to a jsonobject object
Jsonobject jsonobj = jsonobject.Fromobject(Teaobj );
// Step 3: Call the tobean method of jsonobject and convert it into an object
Teacher teac = (teacher) jsonobject.Tobean(Jsonobj, teacher.Class);
System.Out. Println (teac. tostring ());
}
Teacher [age = 28, birth = Tue Feb 28 09: 20: 25cst 2012, name = AA, sex = male, stus = NULL]
// Convert the array string into a collection object
@ Suppresswarnings ("unchecked ")
@ Test
Public voidFromjsonarray (){
// Declare a string
String tew.r = "[{\" Age \ ": 28, \" Birth \ ": {\" date \ ": 28, \" day \ ": 2, \ "hours \": 9, \ "Minutes \": 28, \ "month \": 1, \ "seconds \": 44, \ "time \": 1330392524093, \ "timezoneoffset \":-480, \ "Year \": 112}, \ "Name \": \ "AA \", \ "Sex \": \ "male \" },{ \ "Age \": 28, \ "Birth \": {\ "date \": 28, \ "day \": 2, \ "hours \": 9, \ "Minutes \": 28, \ "month \": 1, \ "seconds \": 44, \ "time \": 1330392524093, \ "timezoneoffset \":-480, \ "Year \": 112}, \ "Name \": \ "AA \", \ "Sex \": \ "male \" },{ \ "Age \": 28, \ "Birth \": {\ "date \": 28, \ "day \": 2, \ "hours \": 9, \ "Minutes \": 28, \ "month \": 1, \ "seconds \": 44, \ "time \": 1330392524093, \ "timezoneoffset \":-480, \ "Year \": 112}, \ "Name \": \ "AA \", \ "Sex \": \ "male \"}] ";
// Call the jsonarray. fromobject method to convert its string into a jsonarray object
Jsonarray jsonarra = jsonarray.Fromobject(Tew.r );
// Step 3: Call
List <teacher> teas = (list <teacher>) jsonarray.Tocollection(Jsonarra,
Teacher.Class);
For(Teacher Tea: TEAs ){
System.Out. Println (tea. tostring ());
}
}
Teacher [age = 28, birth = Tue feb28 09:28:44 CST 2012, name = AA, sex = male, stus = NULL]
Teacher [age = 28, birth = Tue feb28 09:28:44 CST 2012, name = AA, sex = male, stus = NULL]
Teacher [age = 28, birth = Tue Feb 28 09: 28: 44cst 2012, name = AA, sex = male, stus = NULL]
// Convert a Java objectJSONCharacter case
@ Test
Public voidFromobject1 (){
// Convert a Java object to JSON data
Teacher =NewTeacher ();
Teacher. setname ("Old Wang ");
Teacher. setage (23 );
Teacher. setbirth (NewDate ());
List <student> Seas =NewArraylist <student> ();
For(IntI = 0; I <3; I ++ ){
Student Stu =NewStudent ();
Stu. setname ("bear" + I );
Seas. Add (Stu );
}
Teacher. setstus (SEAS );
System.Out. Println (jsonobject.Fromobject(Teacher). tostring ());
}
{"Age": 23, "birth": {"date": 28, "day": 2, "hours": 13, "Minutes": 50, "month ": 1, "seconds": 22, "Time": 1330408222921, "timezoneoffset":-480, "year": 112}, "name": "Old King", "sex ": "", "stus": [{"name": "bear 0" },{ "name": "bear 1" },{ "name ": "Bear 2"}]}