【问题标题】:GSON - Ignore parsing exceptionGSON - 忽略解析异常
【发布时间】:2012-04-02 22:39:04
【问题描述】:

有什么方法可以强制 Gson 忽略任何类型的解析异常(只是跳过该字段)?

【问题讨论】:

  • 你想忽略什么异常?

标签: gson


【解决方案1】:

根据您要查找的字段,您可以不序列化该对象。例如:

    public class test{

    String somString;
    Map<String,String> thisValueThrowsTheError;
    Int somInt;
}

如果您只想忽略 Map 对象,您可以这样做:

    public class test{

    String somString;

    @SerializeName("NOTAVALIDJSONOBJECTNAME")
    Map<String,String> thisValueThrowsTheError;
    Int somInt;
}

Gson 不会看到该 Map 的名称,它会跳过该对象。

或者(最终是更好的解决方案)只是使用反序列化器来正确解决您遇到的问题。请参阅这篇文章作为反序列化器的示例:

Gson deserialization - Trying to parse a JSON to an Object

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多