Use the ReadValue and Writevalueasstring methods of the Jackson Objectmapper object to convert.
For simple primitive types or for objects of type string, use the above method to satisfy.
However, if you want to convert a complex type collection such as List<yourbean>, then you need to deserialize the collection type that is a generic of complex types.
If it is arraylist<yourbean> then use Objectmapper's Gettypefactory (). Constructparametrictype (Collectionclass, elementclasses);
If it is hashmap<string,yourbean> then Objectmapper gettypefactory (). Constructparametrictype (HashMap.class, String.class, Yourbean.class);
As an example, define two models
1 Packagecom.khlin.com.khlin.jackson.json.test;2 3 Importjava.util.List;4 5 Public classStudent {6 7 PrivateString familyname;8 9 PrivateString LastName;Ten One Private intAge ; A - Privatestring[] familymembers; - the PrivatePlace hometown; - - PublicPlace Gethometown () { - returnhometown; + } - + Public voidSethometown (place hometown) { A This. Hometown =hometown; at } - - PublicList<place>getfavoriteplaces () { - returnfavoriteplaces; - } - in Public voidSetfavoriteplaces (list<place>favoriteplaces) { - This. favoriteplaces =favoriteplaces; to } + - PrivateList<place>favoriteplaces; the * PublicString Getfamilyname () { $ returnfamilyname;Panax Notoginseng } - the Public voidsetfamilyname (String familyname) { + This. familyname =familyname; A } the + PublicString Getlastname () { - returnLastName; $ } $ - Public voidSetlastname (String lastname) { - This. LastName =LastName; the } - Wuyi Public intGetage () { the returnAge ; - } Wu - Public voidSetage (intAge ) { About This. Age =Age ; $ } - - Publicstring[] Getfamilymembers () { - returnfamilymembers; A } + the Public voidsetfamilymembers (string[] familymembers) { - This. Familymembers =familymembers; $ } the}
1 Packagecom.khlin.com.khlin.jackson.json.test;2 3 Public classPlace {4 5 PrivateString Province;6 7 PrivateString City;8 9 PublicString getprovince () {Ten returnProvince; One } A - Public voidsetprovince (String province) { - This. Province =Province; the } - - PublicString getcity () { - returnCity ; + } - + Public voidsetcity (String city) { A This. City =City ; at } -}
Conversion code:
1Objectmapper Objectmapper =NewObjectmapper ();2 3Place hometown =NewPlace ();4Hometown.setprovince ("Guangdong");5Hometown.setcity ("Jieyang");6 7Place Place1 =NewPlace ();8Place1.setprovince ("Fujian");9Place1.setcity ("Amoy");Ten OnePlace Place2 =NewPlace (); APlace2.setprovince ("Zhejiang"); -Place2.setcity ("Hangzhou"); - thelist<place> places =NewArraylist<place>(); - Places.add (Place1); - Places.add (PLACE2); -Student Student = generatestudent ("Dacui", "Lin", hometown, places); + -Student Student2 = generatestudent ("Ku", "Lee", hometown, places); + Alist<student> students =NewArraylist<student>(); at Students.add (student); - Students.add (student2); - -String result =objectmapper.writevalueasstring (students); - System.out.println (result); - inJavatype Javatype =objectmapper.gettypefactory () -. Constructparametrictype (ArrayList.class, Student.class); to +List<student> mapperstudents =Objectmapper.readvalue (Result, - javatype); theStudent mapperStudent1 = mapperstudents.get (0); *Student MapperStudent2 = mapperstudents.get (1); $SYSTEM.OUT.PRINTLN ("Success");
Conversion between Java objects and JSON