【问题标题】:Retrofit Android Gson deserializer date inside json在 json 中改造 Android Gson 反序列化器日期
【发布时间】:2016-12-28 01:23:22
【问题描述】:

我有以下问题: 我的 API 的答案总是这样的“风格”:

{
  "type": "dbUserResponse",
  "user": {
    "apellido": "canadas",
    "email": "rj@canada.es",
    "nacimiento": "1995-01-01T00:00:00+01:00",
  }
}

即字段“dbXXXXResponse”和模型。即,字段“响应”和模型。在这种情况下,用户。模型警报的另一个示例:

{
  "type": "dbAlertResponse",
  "alert": {
    "alertId": 0,
    "fecha": "2004-10-19T10:23:54+02:00",
  }
}

Oki...我需要什么?创建类型适配器反序列化或更多? 一个通用的,或者我必须为每个模型制作一个类型适配器。 我做了以下事情:

    public class GenericDeserializer<T> implements JsonDeserializer<T>
    {
@Override
public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
        throws JsonParseException
{
    // Get the "content" element from the parsed JSON
    JsonElement content = je.getAsJsonObject().get("type");


    if(content.toString().equals("\"dbUserResponse\"")){
        content = je.getAsJsonObject().get("user");

        JsonObject jsonObject = content.getAsJsonObject();
        if(jsonObject.has("nacimiento")){
            String date = jsonObject.get("nacimiento").getAsString();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date date2 = null;
            try {
                date2 = sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date2);
            jsonObject.addProperty("nacimiento", newstring);
            content = jsonObject.getAsJsonObject();  
            }
        }

    }
    if(content.toString().equals("\"dbAlertResponse\"")){
    // other case..... etc...

    }

    else{
        //content = '{"error":"mierror"}';
    }


    // Deserialize it. You use a new instance of Gson to avoid infinite recursion
    // to this deserializer
    return new Gson().fromJson(content, type);

}
}

因此,我成功地获得了子 JSON“用户”并在 Android 中填充了我的用户模型。 但是“nacimiento”(日期)字段没有格式化:-(

我还创建了一个 Singleton Retrofit,我不确定它是否正确:

public class SingletonRetrofit {

public static final String BASE_URL = "xxxxxxxxxxxxxxxxxxx";
private static Retrofit retrofit = null;

public static Retrofit getClient() {

    Gson gson = new GsonBuilder()
                    .registerTypeAdapter(User.class, new GenericDeserializer<User>())
                    .create();

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }
    return retrofit;
}
}

我的问题: 1º- 当模型在像我这样的另一个 Json Json 中时,如何正确格式化日期? 2º- 我必须为每个模型创建一个类型适配器吗? :-S 谢谢

【问题讨论】:

    标签: android json gson deserialization retrofit


    【解决方案1】:

    为什么不使用 POJO? JSON 中的日期是字符串,因此您必须将字符串转换为日期。

    【讨论】:

    • 我使用 POJO,当说模型时。当他说模型时,我的意思是 pojo USER。如何将字符串转换为日期??????一旦在日期中,当我应用格式“yyyy-mm-dd”????
    • 如果你使用 POJO,你应该从 API 下载数据,Retrofit 应该将数据解析为 POJO 类,你可以从响应中获取对象作为字符串。
    • 是的,但我的字段“nacimiento”总是以我不想要的格式出现。似乎采用这种格式:Sun Jan 01 00:00:00 GMT + 00:00 1995。我只想出现这个:01-01-1995
    • 你必须格式化日期。你知道日期的原始格式。 1995-01-01T00:00:00+01:00 YYYY-mm-DD HH:MM:SS TZ 你应该格式化为“yyyy-mm-dd”。如果你使用 MVP,你应该在你的 Presenter 中格式化。方式是:Presenter 从 Model 中获取数据。演示者会根据您的需要设置日期格式,并且演示者将数据传递给查看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    相关资源
    最近更新 更多