如何在Spring Boot上将具有数组元素的JSON转换为Java对象

时间:2019-02-27 19:35:12

标签: java json spring spring-boot spring-mvc

我要转换的JSON包含这样的Array元素:

 [{ "_id" : "01001", "city" : "AGAWAM", "loc" : [ -72.622739, 42.070206 ], "pop" : 15338, "state" : "MA" }
,
{ "_id" : "01002", "city" : "CUSHMAN", "loc" : [ -72.51564999999999, 42.377017 ], "pop" : 36963, "state" : "MA" }
,
{ "_id" : "01005", "city" : "BARRE", "loc" : [ -72.10835400000001, 42.409698 ], "pop" : 4546, "state" : "MA" }]

元素loc是一个内部包含两个元素的数组。 我有以下代码将JSON转换为Java对象:

public ModelAndView listCities() throws IOException {
    ModelAndView mav = new ModelAndView(ViewConstant.CITIES);
    ObjectMapper mapper = new ObjectMapper();
    Cities[] obj = mapper.readValue(new File("routeOfTheJSONFile"), Cities[].class);
    mav.addObject("cities", obj);
    return mav;
}

我的实体看起来像这样:

import java.util.Arrays;

public class Cities {
    private String _id;
    private String city;
    private double[] loc;
    private String pop;
    private String state;

    public String get_id() {
        return _id;
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public double[] getLoc() {
        return loc;
    }

    public void setLoc(double[] loc) {
        this.loc = loc;
    }

    public String getPop() {
        return pop;
    }

    public void setPop(String pop) {
        this.pop = pop;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return "Cities{" +
                "_id='" + _id + '\'' +
                ", city='" + city + '\'' +
                ", loc=" + Arrays.toString(loc) +
                ", pop='" + pop + '\'' +
                ", state='" + state + '\'' +
                '}';
    }
}

HTML如下:

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
    <title>Starbucks</title>
</head>
<body>
<div class="container">
    <div class="row justify-content-center">
        <div class="col">
            <table class="table">
                <thead>
                <tr>
                    <th>Id</th>
                    <th>City</th>
                    <th>Location</th>
                    <th>Population</th>
                    <th>State</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="city : ${cities}">
                    <td th:text="${city._id}"></td>
                    <td th:text="${city.city}"></td>
                    <td th:text="${city.loc}"></td>
                    <td th:text="${city.pop}"></td>
                    <td th:text="${city.state}"></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>

</div>


<script src="../js/jquery-3.3.1.min.js"></script>
<script src="../js/popper.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
</body>
</html>

但是当我在表中显示信息时,loc列如下所示:

[D@6481f9f9
[D@41e9a11f
[D@af30d01

It comes in this format

我该如何解决?

1 个答案:

答案 0 :(得分:0)

Cities类添加额外的吸气剂:

String getLocation() {
    return loc[0] + " " + loc[1];
}

在脚本中使用:

<td th:text="${city.location}"></td>

或者,如果您不想创建任何特殊的getter,只需像对loc一样在cities表上进行迭代

编辑
您随时可以使用${#strings.arrayJoin(city.loc, ' ')}方法。

请参阅: