问题
I have tried a lot of samples and tutorials about GSON and how things would work using it such as:
http://www.softwarepassion.com/android-series-parsing-json-data-with-gson/
http://androidsmith.com/2011/07/using-gson-to-parse-json-on-android/
and etc.
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"
}
Another is in this url http://localhost:3000/users.json
which has a json value
[ {
"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.
回答1:
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
}
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);
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!
The previous code fails because Java can't know the class of List<Item>
due to type erasure.
So you have to do it this way:
Type itemListType = new TypeToken<List<Item>>() {}.getType();
List<User> itemList = gson.fromJson(your2stJsonString, itemListType);
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();
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.
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...
来源:https://stackoverflow.com/questions/12568954/trouble-retrieving-data-from-web-service-using-gson