在JSON响应中反序列化日期

在JSON响应中反序列化日期

本文介绍了StackExchange API-在JSON响应中反序列化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用stackexchange api. 在此链接中以获得一些用户信息.

I am trying to use stackexchange api. In this link I am trying to get some users information.

如果运行,将获得JSON响应.

If you run, it you will get the JSON response.

{
  "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"
    },

如果看到的话,日期字段不会反序列化.它给出了数字而不是日期格式.

If you see, the date fields are not deserialized. It gives a number instead of date format.

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

How to get the JSON response in the appropriated date format?

我正在尝试使用在我的Java代码中以JSON形式获取JSON响应,并且可以正常工作.但我想将其解析为一个对象.我必须为项目创建类,并在其中创建具有必需字段的badge_counts.日期字段必须是非整数的日期.从字符串响应解析为对象时,可能存在解析异常.那我该如何解决呢?

I am trying to use this URL in my Java code to get JSON response as String and it's working. But I want to parse it to an object.I have to create class for items, and badge_counts with fields required in it. The date fields must be date not integers.While parsing to object from string response, there may be parsing exception. So how can I resolve that?

推荐答案

从马口中说:"Stack Exchange API中的所有日期均以Unix纪元时间表示,这是自1970年1月1日UTC午夜以来的秒数. Stack Exchange API不接受或返回小数时间,所有内容都应四舍五入到最接近的整数秒."显然,这可以轻松转换为内部使用的任何日期格式.

From the horse's mouth: "All dates in the Stack Exchange API are in unix epoch time, which is the number of seconds since midnight UTC January 1st, 1970. The Stack Exchange API does not accept or return fractional times, everything should be rounded to the nearest whole second." Obviously that can easily converted in whatever date format you are using internally.

这篇关于StackExchange API-在JSON响应中反序列化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:37