SpringMVC整合fastjson解析json出错? 400 报错
先贴出json:
{ "pageNo": 0, "pageSize": 0, "importedBatch": { "billNo": "205-62165364", "voyage": "轮船", "entryId": "123456", "declareEntId": 1, "changed": false, "deleted": false }, "importedParcelMapParcelGoodsList": [ [ { "assBillNo": "900000XXXXX", "receiptNo": "3308021XXXXXXXXXXXX", "recipientName": "陈XX", "recipientIdType": "01", "recipientIdNum": "13XXXXXXXX", "recipientProvinces": "上海市", "recipientPostcode": "200000", "recipientAddress": "上海市闵行区XX路XXX弄XX号XXXX室", "goodsQuantity": 3, "grossWeight": 2.7, "consignerName": "XXXXXXXl Logistics Inc", "consignerAddress": "Unit 8 - XXXXXX", "changed": false, "deleted": false }, [ { "codeTs": "01010700", "goodsName": "2段奶粉XXX", "goodsBrand": "XX", "goodsModel": "XXXG", "unit": "千克", "declQuantity": 1.974, "declPrice": 23.904, "currency": "33080219500322361X", "goodsUrl": "www.gpl.com", "changed": false, "deleted": false } ] ] ] }贴出Java类,以上的json是有以下的Java类对象序列化而成的
public class PaBatchRequest { //页码 private int pageNo; //一页的最大条目数 private int pageSize; //增加的批次对象 private List<Batch> addedBatchList; //删除的批次对象ID private List<Integer> deletedBatchIdList; //更新的批次对象 private List<Batch> updatedBatchList; //被导入的批次对象 private Batch importedBatch; //被导入的Parcel对象和对应的被导入的ParcelGoods对象 // private List<List<Object>> private Map<Parcel, List<ParcelGoods>> importedParcelMapParcelGoodsList; public PaBatchRequest() { } public int getPageNo() { return pageNo; } public PaBatchRequest setPageNo(int pageNo) { this.pageNo = pageNo; return this; } public int getPageSize() { return pageSize; } public PaBatchRequest setPageSize(int pageSize) { this.pageSize = pageSize; return this; } public List<Batch> getAddedBatchList() { return addedBatchList; } public PaBatchRequest setAddedBatchList(List<Batch> addedBatchList) { this.addedBatchList = addedBatchList; return this; } public List<Integer> getDeletedBatchIdList() { return deletedBatchIdList; } public PaBatchRequest setDeletedBatchIdList(List<Integer> deletedBatchIdList) { this.deletedBatchIdList = deletedBatchIdList; return this; } public List<Batch> getUpdatedBatchList() { return updatedBatchList; } public PaBatchRequest setUpdatedBatchList(List<Batch> updatedBatchList) { this.updatedBatchList = updatedBatchList; return this; } public Batch getImportedBatch() { return importedBatch; } public PaBatchRequest setImportedBatch(Batch importedBatch) { this.importedBatch = importedBatch; return this; } public Map<Parcel, List<ParcelGoods>> getImportedParcelMapParcelGoodsList() { return importedParcelMapParcelGoodsList; } public PaBatchRequest setImportedParcelMapParcelGoodsList( Map<Parcel, List<ParcelGoods>> importedParcelMapParcelGoodsList) { this.importedParcelMapParcelGoodsList = importedParcelMapParcelGoodsList; return this; } }服务器端抛出异常:
com.alibaba.fastjson.JSONException.syntax error , expect {, actual [
我想问题应该出在那个Map成员属性上,这段json格式是合法的,客户端使用gson对Java对象进行序列化(也可能因为使用了gson进行序列化,准备把客户端的json处理也换成fastjson试试)。
希望了解这个问题的战友能支招,先谢了。
-----------------------------------------更新分割线-------------------------------------------------------
用fastjson序列化同一个对象,发现序列化出来的json与上面的不一样,json如下
{ "importedBatch": { "billNo": "205-62165364", "changed": false, "dataEditable": true, "declareEntId": 1, "deleted": false, "entryId": "123456", "voyage": "轮船" }, "importedParcelMapParcelGoodsList": { { "assBillNo": "900000164266", "changed": false, "consignerAddress": "XXXX", "consignerName": "Valueway Global Logistics Inc", "dataEditable": false, "deleted": false, "goodsQuantity": 3, "grossWeight": 2.7, "receiptNo": "33080XXXXXXXXXXX", "recipientAddress": "XXXXX", "recipientIdNum": "139XXXXXXXX", "recipientIdType": "01", "recipientName": "陈XX", "recipientPostcode": "200000", "recipientProvinces": "上海市" }: [ { "changed": false, "codeTs": "01010700", "currency": "33080219500322361X", "dataEditable": false, "declPrice": 23.904, "declQuantity": 1.974, "deleted": false, "goodsBrand": "XX", "goodsModel": "XXXG", "goodsName": "2段奶粉XXX", "goodsUrl": "www.XXX.com", "unit": "千克" } ] }, "pageNo": 0, "pageSize": 0 }
看了下你的问题,fastjson默认的map的key是String对象,所以你可以调整下:
privateMap<Parcel, List<ParcelGoods>> importedParcelMapParcelGoodsList;为
private List<ParcelWithListGoods>importedParcelMapParcelGoodsList;
class ParcelWithListGoods{
private Parcel parcel;
private List<ParcelGoods> list;
.........
}
######谢谢您的回复,您的答案给我提供了一个很好的思路######json 的 key 是string,你搞一个 Map<Parcel, List<ParcelGoods>>, key 就是 Parcel 对象 ,就会节外生枝######谢谢您的回复,的确要避免这样复杂的json格式。######importedParcelMapParcelGoodsList属性是Map类型, 对应json数据应该是对象{}而不是数组[]######map中做键值的是对象,序列化出来的对象键值就是对象,值就是数组,很正确啊,不清楚哪里是不合法的######您好,忘了说谢谢,非常感谢您,问题有更新,我用fastjson序列化那个对象,序列化出来的json的确不一样,但还是抛出了同一个异常,而且使用fastjson序列化出来的那段json的格式应该是不合法的,请您看看,麻烦了。######Gson的处理就是把Map属性序列化成数组,每个数组有两个元素,所以Gson对Map的处理不正确或者说不主流?######毛线。importedParcelMapParcelGoodsList 明显是map,格式应该是 {} ,你却弄一个 [] 的list集合类型的结构,不错才有鬼了。"recipientProvinces": "上海市" }: 这里明显是冒号[
"importedParcelMapParcelGoodsList": { { "assBillNo": "900000164266", "changed": false, "consignerAddress": "XXXX", "consignerName": "Valueway Global Logistics Inc", "dataEditable": false, "deleted": false, "goodsQuantity": 3, "grossWeight": 2.7, "receiptNo": "33080XXXXXXXXXXX", "recipientAddress": "XXXXX", "recipientIdNum": "139XXXXXXXX", "recipientIdType": "01", "recipientName": "陈XX", "recipientPostcode": "200000", "recipientProvinces": "上海市" }: [ { "changed": false, "codeTs": "01010700", "currency": "33080219500322361X", "dataEditable": false, "declPrice": 23.904, "declQuantity": 1.974, "deleted": false, "goodsBrand": "XX", "goodsModel": "XXXG", "goodsName": "2段奶粉XXX", "goodsUrl": "www.XXX.com", "unit": "千克" } ] }
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。