问题描述
我正在处理格式不正确的JSON响应.所有字段都以Strings
的形式返回.不幸的是,我无法控制返回数据.
I am working with a JSON response that is improperly formatted. All fields are being returned as Strings
. Unfortunately, I have no control over the return data.
我正在使用Gson并尝试解析一个包含如下字段的JSON对象:
I am using Gson and attempting to parse a JSON object that includes a field like this:
{
[...]
"cost": "9.25"
}
显然应该将其打印为Number
.当我尝试将其解析为String
,Number
或double
时,出现NumberFormatException:
It should obviously be printed as a Number
. When I try to parse this as a String
, Number
or double
I get a 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
LauncherActivity第69行:
LauncherActivity Line 69:
Item item = gson.fromJson(response, Item.class);
所以我遵循了这类似的方法问题,并尝试创建如下的TypeAdapter
:
So I followed this similar question and tried creating a TypeAdapter
like so:
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);
}
}
并在创建GsonBuilder
时注册它:
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Cost.class, new CostTypeAdapter());
Gson gson = builder.create();
还有我的Cost
类:
public class Cost {
private Double value;
public Cost(Double value) {
this.value = value;
}
public Double getValue() {
return value;
}
}
但是我得到相同的NumberFormatException
.
关于这里发生的事情有什么想法吗?至少不应该在我的CostTypeAdapter.deserialize()
中捕获此异常吗?
Any ideas on whats happening here? Shouldn't this exception be caught in my CostTypeAdapter.deserialize()
, at the very least?
非常感谢您的帮助/指导.
Any help/guidance is greatly appreciated.
推荐答案
您还可以使用GsonBuilder
的registerTypeAdapter()
捕获可能的解析异常,并以所需的方式对其进行处理.
You can also use GsonBuilder
's registerTypeAdapter()
to catch possible parsing Exceptions and deal with them the way you want.
在解析Float
并将值设置为null时捕获NumberFormatException
的示例:
Example for catching NumberFormatException
when parsing Float
and make the value 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);
}
});
这篇关于将String转换为double时,GSON中的NumberFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!