java - How can I convert a json string to a bean list? -


i jsonstring:

{    "student[0].firstname":"asdf",    "student[0].lastname":"sfd",    "student[0].gender":"1",    "student[0].foods":[       "steak",       "pizza"    ],    "student[0].quote":"enter favorite quote!",    "student[0].education":"jr.high",    "student[0].tofd":"day",    "student[1].firstname":"sf",    "student[1].lastname":"sdf",    "student[1].gender":"1",    "student[1].foods":[       "pizza",       "chicken"    ],    "student[1].quote":"enter favorite quote!",    "student[1].education":"jr.high",    "student[1].tofd":"night" } 

the student bean:

public class student {     private string firstname;     private string lastname;     private integer gender;     private list<string> foods;     private string quote;     private string education;     private string tofd;     getxxx()...;     setxxx()...; } 

i want use jackson convert jsonstring list。

in fact, want post 2 form content in 1 form,use ajax. , in springmvc handler method, want list or student[] type paramter,then use directly. hope spring can resolver , or use other util jar this.

<form action="" method="post"> first name:<input type="text" name="student[0].firstname" maxlength="12" size="12"/> <br/> last name:<input type="text" name="student[0].lastname" maxlength="36" size="12"/> <br/> gender:<br/> male:<input type="radio" name="student[0].gender" value="1"/><br/> female:<input type="radio" name="student[0].gender" value="0"/><br/> favorite food:<br/> steak:<input type="checkbox" name="student[0].foods" value="steak"/><br/> pizza:<input type="checkbox" name="student[0].foods" value="pizza"/><br/> chicken:<input type="checkbox" name="student[0].foods" value="chicken"/><br/> <textarea wrap="physical" cols="20" name="student[0].quote" rows="5">enter favorite quote!</textarea><br/> select level of education:<br/> <select name="student[0].education">     <option value="jr.high">jr.high</option>     <option value="highschool">highschool</option>     <option value="college">college</option> </select><br/> select favorite time of day:<br/> <select size="3" name="student[0].tofd">     <option value="morning">morning</option>     <option value="day">day</option>     <option value="night">night</option> </select>  first name:<input type="text" name="student[1].firstname" maxlength="12" size="12"/> <br/> last name:<input type="text" name="student[1].lastname" maxlength="36" size="12"/> <br/> gender:<br/> male:<input type="radio" name="student[1].gender" value="1"/><br/> female:<input type="radio" name="student[1].gender" value="0"/><br/> favorite food:<br/> steak:<input type="checkbox" name="student[1].foods" value="steak"/><br/> pizza:<input type="checkbox" name="student[1].foods" value="pizza"/><br/> chicken:<input type="checkbox" name="student[1].foods" value="chicken"/><br/> <textarea wrap="physical" cols="20" name="student[1].quote" rows="5">enter favorite quote!</textarea><br/> select level of education:<br/> <select name="student[1].education">     <option value="jr.high">jr.high</option>     <option value="highschool">highschool</option>     <option value="college">college</option> </select><br/> select favorite time of day:<br/> <select size="3" name="student[1].tofd">     <option value="morning">morning</option>     <option value="day">day</option>     <option value="night">night</option> </select>  <p><input type="submit"/></p> 

your json , if representing collection of students should resemble following. difference collection of student represented json array (as opposed monolithic student1,student2 object etc.)

[     {         "firstname": "asdf",         "lastname": "sfd",         "gender": "1",         "foods": [             "steak",             "pizza"         ],         "quote": "enter favorite quote!",         "education": "jr.high",         "tofd": "day"     },     {         "firstname": "sf",         "lastname": "sdf",         "gender": "1",         "foods": [             "pizza",             "chicken"         ],         "quote": "enter favorite quote!",         "education": "jr.high",         "tofd": "night"     } ] 

if structure , conversion bean 1 liner using google's gson library

gson gson = new gson(); student[] student = gson.fromjson(<your string here>, student[].class); 

now tricky part , have upstream system uses kind of string functions using json libraries , can shell out json provided. have use lot of string manipulation , wish format above not change in tostring() representation of whoever gives data source (there excellent best practice joshua bloch in effective java one).

none less, posting piece of code works (but should never used in form of production quality code). can rolled may single regex , in interest of readability not done. use understanding , because larger collection can choke things up. can explore streaming json parser jackson may , take 1 element @ time , have left out in purpose exercise. hope helps

gson gson = new gson(); string jsoninstring = "{\"student[0].firstname\": \"asdf\",\"student[0].lastname\": \"sfd\",\"student[0].gender\": \"1\",\"student[0].foods\":[\"steak\",\"pizza\"],\"student[0].quote\": \"enter favorite quote!\",\"student[0].education\": \"jr.high\",\"student[0].tofd\": \"day\",\"student[1].firstname\": \"sf\",\"student[1].lastname\": \"sdf\",\"student[1].gender\": \"1\",\"student[1].foods\": [\"pizza\",\"chicken\"],\"student[1].quote\": \"enter favorite quote!\",\"student[1].education\": \"jr.high\",\"student[1].tofd\": \"night\"}"; string jsonwithoutarrayindices = jsoninstring.replaceall("\\[\\d\\]", "").replaceall("student.",""); string jsonascollection = "[" + jsonwithoutarrayindices + "]"; string jsonasvalidcollection = jsonascollection.replaceall(",\"student.firstname\"","},{\"student.firstname\""); system.out.println(jsonasvalidcollection); student[] students = gson.fromjson(jsonasvalidcollection, student[].class); system.out.println("-----------------------------------------------"); system.out.println(students[0]); system.out.println("-----------------------------------------------"); 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -