将String转换为double时GSON中的NumberFormatException

Pau*_*rke 5 java android json gson

我正在使用格式不正确的JSON响应.所有字段都以字段形式返回Strings.不幸的是,我无法控制返回数据.

我正在使用Gson并尝试解析包含如下字段的JSON对象:

{
        [...]
    "cost": "9.25"
}
Run Code Online (Sandbox Code Playgroud)

它显然应该打印成一个Number.当我尝试将其解析为a时String,Number或者double我得到NumberFormatException:

com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: 
        [...]
    at com.myapp.android.LauncherActivity$1.onSuccess(LauncherActivity.java:69)
        [...]
Caused by: java.lang.NumberFormatException: 
    at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
    at java.lang.Double.parseDouble(Double.java:285)
    at com.google.gson.stream.JsonReader.nextInt(JsonReader.java:599)
    at com.google.gson.internal.bind.TypeAdapters$7.read(TypeAdapters.java:228)
    ... 19 more
Run Code Online (Sandbox Code Playgroud)

LauncherActivity第69行:

Item item = gson.fromJson(response, Item.class);
Run Code Online (Sandbox Code Playgroud)

所以我按照这个类似的问题尝试创建一个类似的问题TypeAdapter:

public class CostTypeAdapter implements JsonDeserializer<Double>, JsonSerializer<Double> {

    public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        Double cost;
        try {
            cost = json.getAsDouble();
        } catch (NumberFormatException e) {
            cost = 0.00d;
        }
        return cost;
    }

    public JsonElement serialize(Double src, Type typeOfSrc, 
            JsonSerializationContext context) {
        return new JsonPrimitive(src);
    }
}
Run Code Online (Sandbox Code Playgroud)

并在创建时注册GsonBuilder:

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Cost.class, new CostTypeAdapter());
Gson gson = builder.create(); 
Run Code Online (Sandbox Code Playgroud)

而我的Cost班级:

public class Cost {
    private Double value;

    public Cost(Double value) {
        this.value = value;
    }

    public Double getValue() {
        return value;
    }
}
Run Code Online (Sandbox Code Playgroud)

但我也是这样NumberFormatException.

关于这里发生的任何想法?这个例外难道不应该被我的CostTypeAdapter.deserialize(),至少?

非常感谢任何帮助/指导.

pan*_*dre 10

您还可以使用GsonBuilder's registerTypeAdapter()来捕获可能的解析异常并以您希望的方式处理它们.

NumberFormatException解析时捕获Float并将值设为null的示例:

    GsonBuilder gb = new GsonBuilder();
    gb.registerTypeAdapter(Float.class, new TypeAdapter<Float>() {

        @Override
        public Float read(JsonReader reader) throws IOException {
            if (reader.peek() == JsonToken.NULL) {
                reader.nextNull();
                return null;
            }
            String stringValue = reader.nextString();
            try {
                Float value = Float.valueOf(stringValue);
                return value;
            } catch (NumberFormatException e) {
                return null;
            }
        }

        @Override
        public void write(JsonWriter writer, Float value) throws IOException {
            if (value == null) {
                writer.nullValue();
                return;
            }
            writer.value(value);
        }

    });
Run Code Online (Sandbox Code Playgroud)