使用GSON从网络服务故障检索数据 [英] Trouble retrieving data from web service using GSON
问题描述
我已经尝试了很多关于GSON和样品和教程如何事情会使用它,如工作:
I have tried a lot of samples and tutorials about GSON and how things would work using it such as:
的http:// androidsmith。 COM / 2011/07 /使用-GSON到语法分析JSON-上的Android /
等
我的问题是,我有这个返回值的JSON我的的http://本地主机:3000 /用户/ 1.json
,它是:
My problem is that I have this json value returned by my http://localhost:3000/users/1.json
, which is:
{
"created_at": "2012-09-20T01:43:15Z",
"id": 1,
"name": "kevin",
"updated_at": "2012-09-20T01:43:15Z"
}
还有一个就是在这个网址的http://本地主机:3000 / users.json
里面有一个JSON值
[ {
"created_at": "2012-09-20T01:43:15Z",
"id": 1,
"name": "kevin",
"updated_at": "2012-09-20T01:43:15Z"
}, {
"created_at": "2012-09-20T01:43:33Z",
"id": 2,
"name": "pineda",
"updated_at": "2012-09-20T01:43:33Z"
}, {
"created_at": "2012-09-20T01:46:08Z",
"id": 3,
"name": "raphael",
"updated_at": "2012-09-20T01:46:08Z"
}, {
"created_at": "2012-09-20T16:13:42Z",
"id": 4,
"name": null,
"updated_at": "2012-09-20T16:13:42Z"
}, {
"created_at": "2012-09-20T16:18:03Z",
"id": 5,
"name": null,
"updated_at": "2012-09-20T16:18:03Z"
}, {
"created_at": "2012-09-20T16:19:23Z",
"id": 6,
"name": null,
"updated_at": "2012-09-20T16:19:23Z"
}, {
"created_at": "2012-09-20T16:20:41Z",
"id": 7,
"name": null,
"updated_at": "2012-09-20T16:20:41Z"
}
]
我有一点很难解析这些数据和得到它存储的目的。
I am having a bit of a hard time parsing such data and getting it for storage purposes.
推荐答案
为了解析您的JSON,首先需要创建一个类来的包的数据。你的情况:
In order to parse your JSON, you first need to create a class to wrap your data. In your case:
public class Item {
@SerializedName("created_at")
private String createdAt;
@SerializedName("id")
private int id;
@SerializedName("name")
private String name;
@SerializedName("updated_at")
private String updatedAt;
//getters and setters
}
然后,为了解析您的 1 JSON效应初探,你只需要做的:
Then, in order to parse your 1st JSON reponse, you just have to do:
Gson gson = new Gson();
Item item = gson.fromJson(your1stJsonString, Item.class);
您的第二 JSON的反应是比较麻烦一些,因为它是一个数组。问题是,你不能简单地做:
Your 2nd JSON response is a bit more tricky, because it's an array. The problem is that you can't simply do:
List<Item> item = gson.fromJson(your1stJsonString, List<Item>.class); //wrong!
因为Java不知道班级清单1所述的previous code失败;项目&GT;
因的 类型擦除 。
所以,你必须做这种方式:
So you have to do it this way:
Type itemListType = new TypeToken<List<Item>>() {}.getType();
List<User> itemList = gson.fromJson(your2stJsonString, itemListType);
而这一切,一旦你解析你的目标你的JSON响应(或对象列表),您可以访问所有的数据检索和往常一样:
And that's all, once you have parsed your JSON response in your object (or list of objects) you can access all the retrieved data as usual:
String name = itemList.get(i).getName();
的注1 的:请注意,我已经设置了类型的属性created_at和的updated_at刚才字符串
,因为它使事情变得更加容易。我通常做这种方式,然后我解析日期
或任何其他的 confictive 的类型。无论如何,如果你想直接解析的日期,我想你可以使用自定义解串器下的 GSON的用户指南 。
Note 1: Notice that I've set the types of attributes created_at and updated_at as just String
, because it makes things easier. I usually do it this way, and then I parse the Date
or any other confictive type. Anyway, if you want to directly parse the dates, I think you can use a Custom Deserializer following Gson's User Guide.
的注2 的:使用注释 @SerializedName
的有趣的是,一个字段的名称中的JSON响应,并在您的应用程序分开,为了遵循Java的命名约定...
Note2: The use of the annotation @SerializedName
is interesting to separate the name of a field in the JSON response and in your app, in order to follow Java naming conventions...
这篇关于使用GSON从网络服务故障检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!