public static void main(String[] args) {
ResponseResult r = new ResponseResult();
ResultFaceid rf = new ResultFaceid();
Threshold t = r.new Threshold();
t.setIe_3(1f);
t.setIe_4(2f);
t.setIe_5(3f);
t.setIe_6(4f);
rf.setThresholds(t);
rf.setConfidence(1.2f);
r.setResult_faceid(rf);
r.setTime_userd(1111);
r.setRequest_id("asdfsdf");
String jsonstr = JSON.toJSONString(r);
System.out.println(jsonstr);
Gson gson = new Gson();
gson.fromJson(jsonstr, ResponseResult.class);
JSON.parseObject(jsonstr, ResponseResult.class);
}
上面代码生成的json字符串{"httpStatus":0,"request_id":"asdfsdf","result_faceid":{"confidence":1.2,"thresholds":{"ie_3":1,"ie_4":2,"ie_5":3,"ie_6":4}},"time_userd":1111}
再解析成对象,用gson可以,但用fastjson却报错
Exception in thread "main" com.alibaba.fastjson.JSONException: create instance error, class com.eric.ResponseResult$Threshold
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.createInstance(JavaBeanDeserializer.java:116)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:323)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:135)
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:33)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:420)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:330)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:135)
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:33)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:420)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:330)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:135)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:551)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:250)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:226)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:185)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:303)
at com.eric.ResponseResult.main(ResponseResult.java:48)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.createInstance(JavaBeanDeserializer.java:113)
... 16 more
不知道原因是不是有2层类引用的关系
2条答案
按热度按时间e0uiprwp1#
非静态内嵌类,改成静态就好了
nue99wik2#
@wenshao 错误信息“argument type mismatch”有点误导人