使用resttemplate读取数组没有名称的json对象数组

时间:2015-11-27 05:57:27

标签: json resttemplate

我将从服务器获得响应,如下所示。如何编写类,以便我可以使用resttemplate获取此数据。事情就在这里阵列没有名字。

[
    {
    "place_id": "",
    "licence": "",
    "osm_type": "",
    "osm_id": "",
    "boundingbox": [],
    "lat": "",
    "lon": "",
    "display_name": "",
    "class": "",
    "type": "",
    "icon": 
    "address": {
        "suburb": "",
        "village": "",
        "county": "",
        "state_district": "",
        "state": "",
        "country": "",
        "country_code": ""
        }
    },
    {
    "place_id": "",
    "licence": "",
    "osm_type": "",
    "osm_id": "",
    "boundingbox": [],
    "lat": "",
    "lon": "",
    "display_name": "",
    "class": "",
    "type": "",
    "icon": 
    "address": {
        "suburb": "",
        "village": "",
        "county": "",
        "state_district": "",
        "state": "",
        "country": "",
        "country_code": ""
        }
    }
]

我的控制器类看起来像这样

Addresses[] response = restTemplate.getForObject(url, Addresses[].class);
List<Addresses> objects = new ArrayList<Addresses>()

1 个答案:

答案 0 :(得分:0)

无论你如何称呼外部阶级,这都不重要。我在我的例子中将其称为OuterType:

package hello;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(LowerCaseWithUnderscoresStrategy.class)
public class OuterType {
    // For simplicity properties are public
    public String placeId;
    public String licence;
    public String osmType;
    public String osmId;
    public BoundingBox[] boundingBox;
    public Address address;
}

restTemplate调用:

OuterType[] response = restTemplate.getForObject(url, OuterType[].class);