代码如下
String str = "[{\"amount\":\"1.00\",\"contracts\":\"6000000018911\",\"serialNo\":\"6280329951969281\"}]"; List<TransferSubAccountInfo> list = JSON.parseArray(JsonUtil.toJson(str),TransferSubAccountInfo.class);由于str已经是json格式,如果再次进行toJson虽然str格式没有变化,但是转List的时候会出现如下异常:
Exception in thread "main" com.alibaba.fastjson.JSONException: exepct '[', but string, pos 99, json : "[{\"amount\":\"1.00\",\"contracts\":\"6000000018911\",\"serialNo\":\"6280329951969281\"}]" at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:672) at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:662) at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:657) at com.alibaba.fastjson.JSON.parseArray(JSON.java:514) at com.swk.p2p.proxy.P2pProxy.main(P2pProxy.java:302)正确写法:
String str = "[{\"amount\":\"1.00\",\"contracts\":\"6000000018911\",\"serialNo\":\"6280329951969281\"}]"; List<TransferSubAccountInfo> list = JSON.parseArray(str,TransferSubAccountInfo.class);