Android 使用gson反序列化包含单个列表的对象
我要反序列化以下json:Android 使用gson反序列化包含单个列表的对象,android,rest,gson,deserialization,Android,Rest,Gson,Deserialization,我要反序列化以下json: { "locations": [{ "id": 17, "account_id": 11, "name": "The Haunted Lexington", "radius": 100 }] } (在这个特定的实例中,只有一个位置,但可以有很多) 我使用带有以下代码的Gson反序列化此文件: Gson gson = new GsonBuilder().create(); Location
{
"locations": [{
"id": 17,
"account_id": 11,
"name": "The Haunted Lexington",
"radius": 100
}]
}
(在这个特定的实例中,只有一个位置
,但可以有很多)
我使用带有以下代码的Gson反序列化此文件:
Gson gson = new GsonBuilder().create();
LocationList ll = gson.fromJson(jsonString, LocationList.class);
我定义了以下类:
public class Location {
@SerializedName("id")
private long mId;
@SerializedName("account_id")
private long mAccountId;
@SerializedName("name")
private String mName;
@SerializedName("radius")
private int mRadius;
public long getId() {
return mId;
}
public String getName() {
return mName;
}
}
以及:
有没有办法做到这一点,也许可以提供一个自定义反序列化程序?不久前,我遇到了一个类似的问题,并像这样解决了它
// Parse the JSON response.
JSONArray jsonArray = new JSONArray(response);
List<Location> locations = new ArrayList<Location>();
/*
* Create a Location object for every JSONObject in the response,
* and add it to the list.
*/
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Location location = new Gson().fromJson(jsonObject.toString(),
Location.class);
locations.add(location);
我没有测试最后两行,但我想你明白了
我希望你能用这个来解决你的问题。我目前正在使用一种简单的方法来实现你的目标:
private static <T> List<T> getList(final String jsonVal, final String listTag, T t) {
try {
JsonObject jsonObject = (JsonObject)(new JsonParser()).parse(jsonVal); // root JsonObject. i.e. "locations"
JsonArray jsonArray = (JsonArray)jsonObject.get(listTag);
Gson gson = new GsonBuilder().create();
List<T> list = gson.fromJson(jsonArray, new TypeToken<List<T>>() {}.getType());
return list;
} catch (Exception e) {
throw new IllegalArgumentException("Unexpected json structure!", e);
}
}
private static List getList(最终字符串jsonVal,最终字符串listTag,T){
试一试{
JsonObject JsonObject=(JsonObject)(新的JsonParser()).parse(jsonVal);//根JsonObject,即“位置”
JsonArray JsonArray=(JsonArray)jsonObject.get(listTag);
Gson Gson=new GsonBuilder().create();
List List=gson.fromJson(jsonArray,newTypeToken(){}.getType());
退货清单;
}捕获(例外e){
抛出新的IllegalArgumentException(“意外的json结构!”,e);
}
}
用法示例:
final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson = gsonBuilder.create();
String jsonString = "{\"locations\":[{\"id\":17,\"account_id\":11,\"name\":\"The Haunted Lexington\",\"radius\":100}]}";
List<Location> list = getList(jsonString, "locations", new Location());
final GsonBuilder GsonBuilder=new GsonBuilder();
final Gson Gson=gsonBuilder.create();
字符串jsonString=“{\'位置\”:[{\'id\':17,\'帐户id\':11,\'名称\':\'闹鬼的列克星敦\',\'半径\':100}];
List List=getList(jsonString,“locations”,new Location());
此方法应用于其他类,例如:
List<User> userList = getList(jsonString, "users", new User());
List<Message> messageList = getList(jsonString, "messages", new Message());
List userList=getList(jsonString,“users”,new User());
List messageList=getList(jsonString,“messages”,newmessage());
private static <T> List<T> getList(final String jsonVal, final String listTag, T t) {
try {
JsonObject jsonObject = (JsonObject)(new JsonParser()).parse(jsonVal); // root JsonObject. i.e. "locations"
JsonArray jsonArray = (JsonArray)jsonObject.get(listTag);
Gson gson = new GsonBuilder().create();
List<T> list = gson.fromJson(jsonArray, new TypeToken<List<T>>() {}.getType());
return list;
} catch (Exception e) {
throw new IllegalArgumentException("Unexpected json structure!", e);
}
}
final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson = gsonBuilder.create();
String jsonString = "{\"locations\":[{\"id\":17,\"account_id\":11,\"name\":\"The Haunted Lexington\",\"radius\":100}]}";
List<Location> list = getList(jsonString, "locations", new Location());
List<User> userList = getList(jsonString, "users", new User());
List<Message> messageList = getList(jsonString, "messages", new Message());