【问题标题】:How to parse Json using retrofit and parceler library如何使用改造和 parceler 库解析 Json
【发布时间】:2015-08-10 12:54:03
【问题描述】:

根据这篇博文http://blog.robinchutaux.com/blog/a-smart-way-to-use-retrofit/,我已经读过更好的方法是使用parcelable而不是serialisable进行解析

但是如何使用 Parceler 库 (https://github.com/johncarl81/parceler) 自定义解析?

在 GSON 库中,我们可以这样做:

public class GuestEntityDeserializer implements JsonDeserializer<GuestEntity> {

    private static final String ID = "id";
    private static final String FIRST_NAME = "firstname";
    // other fields...

    @Override
    public GuestEntity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        final GuestEntity guestEntity = new GuestEntity();
        final JsonObject jsonObject = json.getAsJsonObject();

        guestEntity.setId(jsonObject.get(ID).getAsString());
        guestEntity.setFirstName(jsonObject.get(FIRST_NAME).getAsString());
        guestEntity.setLastName(jsonObject.get(LAST_NAME).getAsString());
        // and so on...

        return guestEntity;
    }

并像这样配置 GsonBuilder:

        final Gson gson = new GsonBuilder()
                .registerTypeAdapter(GuestEntity.class, new GuestEntityDeserializer())
                .create();

所以,问题是:如何使用 Retrofit 和 Parceler 库解析 json 字符串?例如,我需要两个具有以下模型的对象。

@Parcel
public class MatchModel {

    @SerializedName("id")
    private String mId;
    @SerializedName("first")
    private String mFirst;
    @SerializedName("last")
    private String mLast;
    @SerializedName("ilike")
    private String mIlike;
    @SerializedName("created_at")
    private String mCreatedAt;
    @SerializedName("liketo")
    private String mLiketo;
    @SerializedName("firstname")
    private String mFirstName;
    @SerializedName("lastname")
    private String mLastName;
    @SerializedName("birthday")
    private String mBirthday;

    //  getters and setters
}

我需要解析这个:

{
  "1277": {
    "id": "322",
    "first": "1294",
    "last": "1277",
    "ilike": "1",
    "created_at": "2015-07-31 18:51:47",
    "liketo": "1277",
    "firstname": "Alex",
    "lastname": "Alexov",
    "birthday": null
  },
  "1312": {
    "id": "951",
    "first": "1294",
    "last": "1312",
    "ilike": "1",
    "created_at": "2015-08-06 15:12:29",
    "liketo": "1312",
    "firstname": "Roman",
    "lastname": "Romanov",
    "birthday": "1990-06-23"
  }
}

【问题讨论】:

    标签: android retrofit parceler


    【解决方案1】:

    使用改造解析

    RestAdapter restAdapter = new RestAdapter.Builder()
       .setEndpoint("url")
       .setClient(new OkClient(new OkHttpClient()))
       .build();
    
    getMatchModel mMatchModel= restAdapter.create(getMatchModel.class);
    Map<String, MatchModel > mMatchModelMaps = mMatchModel.MatchModel(params..);
    
    
    public interface getMatchModel {
       @GET("/{params}")
       Map<String, MatchModel > MatchModel(@Path("params") String params);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 2023-03-23
      • 2016-12-19
      相关资源
      最近更新 更多