0

嗨,我有以下 JSON

{
 "code": 0,
 "response": {
  "userObject": {
   "User": {
    "id": "355660",
    "first_name": "Dummy",
    "last_name": "dummy",
    "email": "dumb@email.com",
    "birthday": "2012-05-07",
    "created": "2012-08-21 06:41:05",
    "modified": "2012-08-21 06:41:05",
    "image_url": null,
   },
   "Location": {
    "id": "273550",
    "name": "New York City",
    "asciiName": "New York City",
    "lat": "40.714272",
    "lon": "-74.005966",
    "geoname_modified": "2011-11-08 00:00:00",
    "timeZone": "America/New_York",
    "countryName": "United States",
    "state": "New York",
    "created": "2012-07-12 12:11:01",
    "modified": "2012-08-20 14:27:24"
   }
  }
 }
}

我有两门课,一门用于 Location User

我知道如果我创建嵌套类,我可以获得对象

    response
     ->UserObject
         *User
         *Location

但我不想创建两个额外的类来 UserObject 包装 response两个 POJO。有没有更简单的方法呢??

我正在Jackson Parser 使用Spring for android

4

2 回答 2

4

如果您真的想避免一次性课程,您也可以分两步完成,例如:

JsonNode tree = mapper.readTree(...);
User user = mapper.treeToValue(tree.path("response").path("userObject").get("User"), User.class);
Location loc = mapper.convertValue(tree.path("response").path("userObject").get("Location"), Location.class);

但是是的,我可能会选择愚蠢的结构类:

static class Response {
  public UserObject userObject;
}
static class UserObject {
  public Location Location;
  public User User;
}

因为它真的没有更多的代码。

于 2012-08-21T19:54:27.543 回答
2

classes您可以创建或arrays使用hashmap. 就个人而言,我只会创建classes. 我认为这为您的应用程序提供了更大的灵活性,并使您可以更轻松地处理对象。我知道设置它们需要时间,但是一旦你这样做了,你就可以使用ArrayList并且你可以更容易地解析它们JSON

于 2012-08-21T12:41:44.607 回答