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

通过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的顺序

你可能感兴趣的:(java,servlet,jvm)