如何将json字符串转换为bean列表?

时间:2016-08-24 03:29:55

标签: java json spring spring-mvc

我得到一个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"
}

学生豆:

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()...;
}

我想使用jackson将JsonString转换为List。

事实上,我想在一个表单中发布两个表单内容,使用ajax。 在SpringMVC处理程序方法中,我希望得到List或Student []类型参数,然后直接使用。我希望春天可以解决它,或者使用其他的工具罐来做到这一点。

<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>

1 个答案:

答案 0 :(得分:0)

您的JSON,如果它代表一组学生应该类似于以下内容。区别在于这是一个表示为JSON数组的学生集合(而不是整体的student1,student2对象等)。

[
    {
        "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"
    }
]

如果这是结构,转换为bean是使用Google的GSON库的单线程

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

现在是棘手的部分,我有一个上游系统使用某种字符串函数而不是使用JSON库,并且可以像你提供的那样发出JSON。您将不得不使用大量的字符串操作,并且还希望上面的格式不会在任何从源代码中提供数据的toString()表示中更改(Joshua Bloch在Effective Java中有一个关于此的优秀最佳实践一个)。

尽管如此,我发布了一段有效的代码(但绝不应该用于任何形式的生产质量代码)。这可以卷起来作为单个正则表达式,为了便于阅读,它没有完成。使用它只是为了理解,因为更大的集合可以迅速扼杀。你也可以像JACKSON一样探索一个流式JSON解析器,并一次把它作为一个元素,我把它留作目的作为练习。希望这有帮助

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("-----------------------------------------------");