如何检索JSON对象列表

时间:2017-03-10 15:53:42

标签: java json rest serialization

当我访问此URI时,我得到了一个Json对象列表:

personUri = https://myuri.com/get/listOfPeople/

JSON

[{"firstName":"Tom","lastName":"Blue"},{"firstName":"Susan","lastName":"Summers"},{"firstName":"Albert","lastName":"Smith"}]

爪哇:

List<People>persons = new ArrayList<>();
UriComponentsBuilder componentsBuilder = UriComponentsBuilder.fromHttpUrl(personUri);
String jsonInfo = getRestTemplate().getForEntity(componentsBuilder.toUriString(), String.class).getBody();
LOG.debug("REST RESPONSE: "+jsonInfo);
Gson gson = new Gson();
Type type=new TypeToken<List<People>>(){}.getType();
persons=gson.fromJson(jsonInfo,type);

我收到此错误。
预计BEGIN_ARRAY但在第1行第62列STRING

这是因为我需要使用HashMap.class而不是String.class吗?

VO

public class People {

private String firstName;
private String lastName;

public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}

}

0 个答案:

没有答案