Jackson provides interfaces that can be converted between JSON and beans
1. An example
Public classJsontojavabean { Public Static voidMain (string[] args) {String str= "{\" student\ ": [{\" name\ ": \" leilei\ ", \" age\ ": 23},{\" name\ ": \" leilei02\ ", \" age\ ": 23}]}"; Student Stu=NULL; List<Student> list =NULL; Try{objectmapper Objectmapper=NewObjectmapper (); Studentlist studentlist=objectmapper.readvalue (str, studentlist.class); List=studentlist.getstudent (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } for(Student s:list) {System.out.println (S.getname ()+" "+s.getage ()); } }}
2. A second example
Public Static voidMain (string[] args) {ArrayList<Student> list=NewArraylist<student>(); Student S1=NewStudent (); S1.setname ("Leilei"); S1.setage (23); Student S2=NewStudent (); S2.setname ("Leilei02"); S2.setage (23); List.add (S1); List.add (S2); StringWriter Str=NewStringWriter (); Objectmapper Objectmapper=NewObjectmapper (); Try{objectmapper.writevalue (str, list); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println (str); }
The usage I see
Public throws jsonparseexception, Jsonmappingexception, IOException { returnnew objectmapper (). ReadValue (JSON, Session. ) Class); }
Summarize
1. JSON exists as a string in Java
2. The information required for the conversion is the original JSON data and the definition of the transformation standard bean
3. You can convert an array and a single object