问题
I am trying to use stackexchange api. In this link I am trying to get some users information.
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.
How to get the JSON response in the appropriated date format?
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?
回答1:
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.
回答2:
Sometime back I happened to play around with this very thing. The date related fields you see provide the values in milliseconds(since the Epoch, UTC Jan 1st, 1970), which is of the long
type and not Integer
. But since I wanted to use Date
in my mini application, this is the way I went about designing the date fields in my model class.
// 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 :)
}
来源:https://stackoverflow.com/questions/23380182/stackexchange-api-deserialize-date-in-json-response