3

我正在尝试使用 stackexchange api。 在此链接中,我试图获取一些用户信息。

如果你运行它,你会得到 JSON 响应。

{
  "items": [
    {
      "badge_counts": {
        "bronze": 5630,
        "silver": 4212,
        "gold": 267
      },
      "account_id": 11683,
      "is_employee": false,
      "last_modified_date": 1398827800,
      "last_access_date": 1398799412,
      "reputation_change_year": 34829,
      "reputation_change_quarter": 7965,
      "reputation_change_month": 7965,
      "reputation_change_week": 930,
      "reputation_change_day": 60,
      "reputation": 669736,
      "creation_date": 1222430705,
      "user_type": "registered",
      "user_id": 22656,
      "age": 37,
      "accept_rate": 88,
      "location": "Reading, United Kingdom",
      "website_url": "http://csharpindepth.com",
      "link": "http://stackoverflow.com/users/22656/jon-skeet",
      "display_name": "Jon Skeet",
      "profile_image": "https://www.gravatar.com/avatar/6d8ebb117e8d83d74ea95fbdd0f87e13?s=128&d=identicon&r=PG"
    },
    {
      "badge_counts": {
        "bronze": 1646,
        "silver": 1456,
        "gold": 64
      },
      "account_id": 14332,
      "is_employee": false,
      "last_modified_date": 1397859689,
      "last_access_date": 1398787554,
      "reputation_change_year": 26427,
      "reputation_change_quarter": 5693,
      "reputation_change_month": 5693,
      "reputation_change_week": 640,
      "reputation_change_day": 20,
      "reputation": 513076,
      "creation_date": 1224432467,
      "user_type": "registered",
      "user_id": 29407,
      "age": 32,
      "accept_rate": 91,
      "location": "Sofia, Bulgaria",
      "website_url": "http://stackoverflow.com/search?q=user%3a29407&tab=newest",
      "link": "http://stackoverflow.com/users/29407/darin-dimitrov",
      "display_name": "Darin Dimitrov",
      "profile_image": "https://www.gravatar.com/avatar/e3a181e9cdd4757a8b416d93878770c5?s=128&d=identicon&r=PG"
    },

如果您看到,日期字段未反序列化。它给出了一个数字而不是日期格式。

如何以适当的日期格式获取 JSON 响应?

我正在尝试在我的 Java 代码中使用这个 URL来获取 JSON 响应作为字符串并且它正在工作。但我想将它解析为一个对象。我必须为项目创建类,并创建带有所需字段的 badge_counts。日期字段必须是日期而不是整数。从字符串响应解析到对象时,可能会出现解析异常。那么我该如何解决呢?

4

2 回答 2

2

从马的嘴里说:“Stack Exchange API 中的所有日期都是 unix 纪元时间,即自 1970 年 1 月 1 日午夜 UTC 以来的秒数。Stack Exchange API 不接受或返回小数时间,所有内容都应四舍五入为最接近的整秒。” 显然,这可以轻松转换为您在内部使用的任何日期格式。

于 2014-04-30T06:01:17.003 回答
0

前一段时间,我碰巧玩弄了这个东西。您看到的与日期相关的字段以毫秒为单位提供值(自 1970 年 1 月 1 日 UTC 纪元以来),它是longtype 而不是Integer. 但是因为我想Date在我的迷你应用程序中使用,所以我在我的模型类中设计日期字段的方式是这样的。

// Example for one field - can be extended for all other Date related fields.

private Long last_access_date; // Let this be as long or Long (your need)

public void setLast_access_date(Long last_access_date) { // The json parsers use the setter method to set the value
    this.last_access_date = last_access_date; // the long value is set, no issues here :)
}

public Date getLast_access_date() { // you use the getter in your code
    return new Date(last_access_date); // return a new date using the milliseconds and have a Date in your application to display :)
}
于 2014-04-30T05:23:47.643 回答