JSONUtil格式化实体对像时顺序不一致问题

文章探讨了使用JSONUtil.toJsonStr()和JSON.toJSONString()对实体对象进行序列化时,出现的字段顺序不一致的情况。在第一个示例中,SubImageInfoObject的顺序与预期不符,而在第二个使用JSON.toJSONString()的示例中,顺序是正常的。这表明JSONUtil.toJsonStr()可能不保证字段的原始顺序。
摘要由CSDN通过智能技术生成

通过JSONUtil.toJsonStr()格式化实体对象,出现顺序不一致的情况。

        List<SubImageInfo> subImageInfoList = new ArrayList<>();
        subImageInfoList.add(subImageInfo);
        subImageList.setSubImageInfoObject(subImageInfoList);
        face.setSubImageList(subImageList);
        faceList.add(face);
        faceListObject.setFaceObject(faceList);
        String  s=JSONUtil.toJsonStr(faceRequestObject);
        System.out.println(s);
{
	"FaceListObject": {
		"FaceObject": [{
			"FaceID": "660000000011901234560220171201101010000070600001",
			"DeviceID": "37140020225036153919",
			"SourceID": "66000000001190123456022017120110101000007",
			"IsVictim": 0,
			"SubImageList": {
				"SubImageInfoObject": [{
					"DeviceID": "37140020225036153919",
					"EventSort": 2,
					"Data": "",
					"FileFormat": "Jpeg",
					"Type": "11",
					"ImageID": "66000000001190123456022017120110101000007",
					"ShotTime": "20230606202348",
					"Height": 441,
					"Width": 358
				}]
			},
			"LeftTopY": 1,
			"LeftTopX": 1,
			"IsSuspectedTerrorist": 0,
			"IsForeigner": 0,
			"InfoKind": 0,
			"RightBtmY": 1,
			"RightBtmX": 1,
			"IsCriminalInvolved": 0,
			"IsDetainees": 0,
			"IsSuspiciousPerson": 0
		}]
	}
}

在试下通过JSON.toJSONString转换

        List<SubImageInfo> subImageInfoList = new ArrayList<>();
        subImageInfoList.add(subImageInfo);
        subImageList.setSubImageInfoObject(subImageInfoList);
        face.setSubImageList(subImageList);
        faceList.add(face);
        faceListObject.setFaceObject(faceList);
        String  s= JSON.toJSONString(faceListObject);
        System.out.println(s);
{
	"faceObject": [{
		"deviceID": "37140020225036153919",
		"faceID": "660000000011901234560220171201101010000070600001",
		"infoKind": 0,
		"isCriminalInvolved": 0,
		"isDetainees": 0,
		"isForeigner": 0,
		"isSuspectedTerrorist": 0,
		"isSuspiciousPerson": 0,
		"isVictim": 0,
		"leftTopX": 1,
		"leftTopY": 1,
		"rightBtmX": 1,
		"rightBtmY": 1,
		"sourceID": "66000000001190123456022017120110101000007",
		"subImageList": {
			"subImageInfoObject": [{
				"data": "",
				"deviceID": "37140020225036153919",
				"eventSort": 2,
				"fileFormat": "Jpeg",
				"height": 441,
				"imageID": "66000000001190123456022017120110101000007",
				"shotTime": "20230606202642",
				"type": "11",
				"width": 358
			}]
		}
	}]
}

正常

所以Map对象使用attributes为key时JSONUtil.toJsonStr()序列化会改变key的顺序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值