我有一个pojo,它应该是一个json字符串表示
public class MyIncomingJsonLineItem implements Wrapper {
private static final long serialVersionUID = 8902983775804518593L;
@SerializedName("ProductName")
private String productName=null;// => 'Watch',
@SerializedName("ProductDescription")
private String productDescription=null;// => 'Nice watch',
@SerializedName("ProductSellingPrice")
private BigDecimal productSellingPrice=null;//' => 99.99,
@SerializedName("ProductQty")
private Integer productQty=null;// => 2,
@SerializedName("ProductCostPrice")
private BigDecimal productCostPrice=null;//
...
}
我有一个json字符串,其中ProductCostPrice =""和ProductQty =" AA"
然后我想要一个填充的对象:
MyIncomingJsonLineItem ww=gson.fromJson(jsonString,MyIncomingJsonLineItem.class);
我得到许多烦人的异常,我的字符串不是数字,依此类推等等。而且我没有能力知道哪些是错误的,哪个领域等等。但这是一个谷歌文库,我无法相信我无法全面了解什么是'继续
我像这样创建gson:
GsonBuilder gb = new GsonBuilder();
gb.serializeNulls();
Gson gson = gb.create();
我还添加了一个自定义BigDecimal反序列化适配器,但是我应该为所有类型创建这样的适配器,包括内置?我只是厌倦了这个琐碎的功能。
所以问题是:
更新
原始例外:
com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String
at com.google.gson.internal.bind.TypeAdapters$8.read(TypeAdapters.java:265) ~[gson-2.4.jar:na]
at com.google.gson.internal.bind.TypeAdapters$8.read(TypeAdapters.java:255) ~[gson-2.4.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117) ~[gson-2.4.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217) ~[gson-2.4.jar:na]
at com.google.gson.Gson.fromJson(Gson.java:814) ~[gson-2.4.jar:na]
at com.google.gson.Gson.fromJson(Gson.java:779) ~[gson-2.4.jar:na]
at com.google.gson.Gson.fromJson(Gson.java:728) ~[gson-2.4.jar:na]
at com.google.gson.Gson.fromJson(Gson.java:700) ~[gson-2.4.jar:na]
at com.myapp.workers.core.WorkerHelper.convertFromPlainJsonPostString(WorkerHelper.java:348) ~[workers-core-1.0-SNAPSHOT.jar:na]
at com.myapp.workers.core.CommonListener.lambda$onMessage$1(CommonQueueListener.java:176) ~[workers-core-1.0-SNAPSHOT.jar:na]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_77]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_77]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_77]
Caused by: java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842) ~[na:1.8.0_77]
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) ~[na:1.8.0_77]
at java.lang.Double.parseDouble(Double.java:538) ~[na:1.8.0_77]
at com.google.gson.stream.JsonReader.nextLong(JsonReader.java:977) ~[gson-2.4.jar:na]
at com.google.gson.internal.bind.TypeAdapters$8.read(TypeAdapters.java:263) ~[gson-2.4.jar:na]
... 12 common frames omitted