开发者社区> 问答> 正文

SpringMVC整合fastjson解析json出错? 400 报错

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
}



    但抛出了同样一个异常。

展开
收起
爱吃鱼的程序员 2020-06-22 23:09:00 679 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    看了下你的问题,fastjson默认的map的key是String对象,所以你可以调整下:

     privateMap<Parcel,List<ParcelGoods>>importedParcelMapParcelGoodsList;为

    privateList<ParcelWithListGoods>importedParcelMapParcelGoodsList;

    classParcelWithListGoods{

    privateParcelparcel;

    privateList<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集合类型的结构,不错才有鬼了。
    ######回复 @sxgkwei:谢谢您,其实是使用原生支持的jackson时出现问题才换的fastjson,问题解决了,客户端用的gson,服务器端用的fastjson,导致反序列化出现错误。######回复 @VVL:springMVC默认的整合json处理jar,好像叫jackjson,你搜一下,只要把这个jar包放进项目,springmvc好像不需要配置,它都会自动直接调用的。不要费劲搞一下有的没的的东西,关注你们系统业务本身,这种东西,直接用现成的方案。######您好,谢谢您的回复,您看到我用fastjson序列化的那段json了吗?那段json也有问题,不知道怎样解决这个问题。######需要Json与类相互转换时,最好不要定义像Map一样的复杂成员,几大常用工具都有问题,可以把几大工具都试一下,不行的话最好修改成员,像@混元归一的答案那种######谢谢您的回复,的确最好不要那么复杂,我这个问题主要是客户端与服务器端用的json处理包不一样,导致反序列化出现错误。######哎,哥们你属于马虎类型,第一段贴出的JSON明显与最后贴出的格式不样。下面是你最后贴出的正常的JSON, "recipientProvinces":"上海市"}:这里明显是冒号[ "importedParcelMapParcelGoodsList":{{"assBillNo":"900000164266","changed":false,"consignerAddress":"XXXX","consignerName":"ValuewayGlobalLogisticsInc","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":"千克"}]}



    ######谢谢您的回复,我知道两段不一样,上面那段是Gson序列化而成的,下面那段是fastjson序列化而成的,问题解决了,客户端用的gson,服务器端用的fastjson,导致反序列化出现错误。###### 直接用spring的jackjson方便的多,后期数据量大的时候自己在做优化######谢谢您,其实是之前使用原生支持的jackson时出现问题才换的fastjson,问题解决了,客户端用的gson,服务器端用的fastjson,导致反序列化出现错误。
    2020-06-22 23:09:19
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关镜像