2

我正在使用 Spring for Android 并通过以下方式提取 JSON 数据:

课程活动.java

private void getCourses()
{
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
  String url = "http://192.168.1.74:3000/andy/courses.json";
  Course[] coursesArray = restTemplate.getForObject(url, Course[].class);

  ...
}

课程.java

@JsonIgnoreProperties(ignoreUnknown = true)
public class Course implements Parcelable, DBObject
{
    @JsonProperty
    private String number = null;
    @JsonProperty
    private String title = null;
    @JsonProperty
    private int school_id = -1;
    @JsonProperty
    private String department = null;

    ...
}

JSON 调用返回这种类型的数据:

[{"id":1,"number":"CE-490","title":"Senior Design","school_id":1,"department":"Computer Engineering"},{"id":2,"number":"CE-491","title":"Introduction to Mobile App Development","school_id":1,"department":"Computer Engineering"}]

现在您可以看到 JSON 数据键直接映射到我的课程属性名称。

如果我想有不同的名字怎么办?具体来说,如果我的 JSON 返回诸如“notify_on_follow”之类的键,我如何使用 Spring for Android 将其映射到不带下划线的“notifyOnFollow”?或者也许将“active”更改为“isActive”?

4

1 回答 1

3

我找不到文档,但我很确定这会起作用:

@JsonProperty("notify_on_follow")
private SomeType notifyOnFollow;
于 2012-11-18T02:40:59.440 回答