1

I get a JsonString:

{
   "student[0].firstName":"asdf",
   "student[0].lastName":"sfd",
   "student[0].gender":"1",
   "student[0].foods":[
      "Steak",
      "Pizza"
   ],
   "student[0].quote":"Enter your 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 your 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 to use jackson to convert the JsonString to List。

In fact, I want to post two form content in one form,use ajax. And in the SpringMVC handler method, i want get like List or Student[] type paramter,then use directly. I hope spring can resolver it , or use other util jar do 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 your favorite quote!</textarea><br/>
Select a 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 your 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 your favorite quote!</textarea><br/>
Select a 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 your 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>

solverpeng
  • 47
  • 1
  • 10
  • 1
    What a horrid piece of JSON. You'll likely need to deserialize the JSON into a normal HashMap (or JObject or whatever, using whatever library you've selected) and then manipulate into the DTOs appropriately. – user2864740 Aug 24 '16 at 03:31
  • possible for duplicate http://stackoverflow.com/questions/9829403/deserialize-json-to-arraylistpojo-using-jackson – Shakir Ahamed Aug 24 '16 at 03:40
  • 4
    Are you able to change the JSON you are receiving? Your JsonString right now is representing one large item i.e. {student1, student2} when it should be an array of n items {[{student1},{student2}]}. – Billy Korando Aug 24 '16 at 03:43
  • I supplementary the question.@Billy Korando – solverpeng Aug 25 '16 at 06:05

1 Answers1

0

Your JSON , if it is representing a collection of students should resemble the following. The difference is this is a collection of student represented as a JSON Array (as opposed to a monolithic student1,student2 object etc.)

[
    {
        "firstName": "asdf",
        "lastName": "sfd",
        "gender": "1",
        "foods": [
            "Steak",
            "Pizza"
        ],
        "quote": "Enter your favorite quote!",
        "education": "Jr.High",
        "tOfD": "Day"
    },
    {
        "firstName": "sf",
        "lastName": "sdf",
        "gender": "1",
        "foods": [
            "Pizza",
            "Chicken"
        ],
        "quote": "Enter your favorite quote!",
        "education": "Jr.High",
        "tOfD": "Night"
    }
]

If this is the structure , conversion to the bean is a one liner using Google's GSON library

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

Now is the tricky part , i have an upstream system which uses some kind of string functions than using JSON libraries and can shell out JSON as you provided. You will have to use a lot of string manipulation and also wish that the format above does not change in the toString() representation of whoever gives you the data from the source (There is an excellent best practice by Joshua Bloch in Effective Java about this one).

None the less, i am posting a piece of code that works (but should never be used in any form of production quality code). This can be rolled up may be as a single regex , in the interest of readability it is not done. Use this for understanding only , because a larger collection can quickly choke things up. You can also explore a Streaming JSON parser like JACKSON may be and take it one element at a time , i have left that out in purpose as an exercise. Hope this 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 your 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 your 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("-----------------------------------------------");
Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24