对象的包裹列表GSON反序列化 [英] GSON deserialization of a wrapped list of objects

查看:231
本文介绍了对象的包裹列表GSON反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图反序列化从一个JSON响应对象列表。 JSON数组有一个键,这是造成使用GSON反序列化它的问题。

我也与此类似20的对象。

 公共类设备扩展实体{
  串DEVICE_ID;
  串DEVICE_TYPE;
  串device_push_id;
}

有关最有返回对象的列表的API方法。返回的JSON看起来是这样的。因为其他客户的,改变的JSON格式不此时一个合理的选择。

  {
   设备:[
      {
         ID:Y3mK5Kvy
         DEVICE_ID:did_e3be5,
         DEVICE_TYPE:IOS
      },
      {
         ID:6ZvpDPvX
         DEVICE_ID:did_84fdd,
         DEVICE_TYPE:机器人
      }
   ]
}

为了解析这个类型我目前使用的 org.json 方法和GSON。

混合反应

  JSONArray jsonResponse =新的JSONObject(响应).getJSONArray(设备);键入deviceListType =新TypeToken<名单,LT;装置>>(){} .getType();
ArrayList的<装置>器件= gson.fromJson(jsonResponse.toString(),deviceListType);

我要找的做反序列化,我想使用改造清洁方法。在答案<一个href=\"http://stackoverflow.com/questions/23070298/get-nested-json-object-with-gson-using-retrofit\">Get使用GSON改造嵌套的JSON对象接近我所需要的,但不处理列表秒。我在这里抄答案的通用版本:

 公共类RestDeserializer&LT; T&GT;实现JsonDeserializer&LT; T&GT; {
  私有类&LT; T&GT; mClass;
  私人字符串MKEY;  公共RestDeserializer(类&LT; T&GT; targetClass,串键){
    mClass = targetClass;
    MKEY =键;
  }  @覆盖
  公共ŧ反序列化(JsonElement jsonElement,类型类型,JsonDeserializationContext jsonDeserializationContext)
    抛出JsonParseException {    JsonElement值= jsonElement.getAsJsonObject()获得(MKEY)。
    如果(值!= NULL){
      返回新GSON()fromJson(值,mClass)。
    }其他{
      返回新GSON()fromJson(jsonElement,mClass)。
    }
  }
}

我的目标是让这个叫只是工作。

  @GET(/ API / V1 /保护/设备)
公共无效getDevices(回拨&LT;名单,LT;装置&gt;&GT;回调);


解决方案

使用下面的类

 公共类设备{@暴露
私人列表&LT;装置&gt;设备=新的ArrayList&LT;装置&gt;();/ **
*
* @返回
*器件
* /
公开名单&LT;装置&gt; getDevices(){
返回装置;
}/ **
*
* @参数设备
*器件
* /
公共无效setDevices(列表&LT;装置&gt;装置){
this.devices =设备;
}}

设备类

 公共类设备扩展实体{
  @暴露
  字符串ID;
   @暴露
  串DEVICE_ID;
  @暴露
  串DEVICE_TYPE;
}

 公共类设备扩展实体{
  @expose @SerializedName(ID)
  字符串的DeviceID;
   @expose @SerializedName(DEVICE_ID)
  串devicePushId;
  @expose @SerializedName(DEVICE_TYPE)
  字符串的devicetype;
}

更新改造方法

  @GET(/ API / V1 /保护/设备)
公共无效getDevices(回拨&LT;设备和GT;回调);devices.getDevices()//回调方法里面打电话给你名单

另外,你不会需要自定义解串器

I am trying to de-serialize a list of objects from a JSON response. The JSON array has a key, which is causing issues with using GSON to de-serialize it.

I have about 20 objects similar to this.

public class Device extends Entity {
  String device_id;
  String device_type;
  String device_push_id;
}

For most there is an API method which returns a list of objects. The returned JSON looks like this. Because of other clients, changing the format of the JSON is not a reasonable option at this point.

{
   "devices":[
      {
         "id":"Y3mK5Kvy",
         "device_id":"did_e3be5",
         "device_type":"ios"
      },
      {
         "id":"6ZvpDPvX",
         "device_id":"did_84fdd",
         "device_type":"android"
      }
   ]
}

In order to parse this type of response I'm currently using a mix of org.json methods and Gson.

JSONArray jsonResponse = new JSONObject(response).getJSONArray("devices");

Type deviceListType = new TypeToken<List<Device>>() {}.getType();
ArrayList<Device> devices = gson.fromJson(jsonResponse.toString(), deviceListType);

I'm looking for a cleaner method of doing the deserialization as I'd like to use Retrofit. The answer in Get nested JSON object with GSON using retrofit is close to what I need, but doesn't handle Lists. I've copied the generic version of the answer here:

public class RestDeserializer<T> implements JsonDeserializer<T> {
  private Class<T> mClass;
  private String mKey;

  public RestDeserializer(Class<T> targetClass, String key) {
    mClass = targetClass;
    mKey = key;
  }

  @Override
  public T deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
    throws JsonParseException {

    JsonElement value = jsonElement.getAsJsonObject().get(mKey);
    if (value != null) {
      return new Gson().fromJson(value, mClass);
    } else {
      return new Gson().fromJson(jsonElement, mClass);
    }
  }
}

My goal is to have this call "just work".

@GET("/api/v1/protected/devices")
public void getDevices(Callback<List<Device>> callback);

解决方案

Use the below class

public class Devices {

@Expose
private List<Device> devices = new ArrayList<Device>();

/**
* 
* @return
* The devices
*/
public List<Device> getDevices() {
return devices;
}

/**
* 
* @param devices
* The devices
*/
public void setDevices(List<Device> devices) {
this.devices = devices;
}

}

Device class

public class Device extends Entity {
  @Expose
  String id;
   @Expose
  String device_id;
  @Expose
  String device_type;
}

or

public class Device extends Entity {
  @Expose @SerializedName("id")
  String deviceId;
   @Expose @SerializedName("device_id")
  String devicePushId;
  @Expose @SerializedName("device_type")
  String deviceType;
}

update retrofit method to

@GET("/api/v1/protected/devices")
public void getDevices(Callback<Devices> callback);

devices.getDevices() //call inside callback method will give you the list

Also, you wont require the custom deserializer

这篇关于对象的包裹列表GSON反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆