[alibaba/fastjson]map扩容的时候存在顺序改变

2025-11-11 855 views
4

在1.8环境下,参数数量大于12时出现扩容,之后删除两个元素到10个,之后重构这json对象,会发生json数据与新建json顺序不一致的问题

回答

0

直接放入12个元素的顺序 { "legalName": "4", "linkPhone": "6", "entBankPhone": "11", "entName": "2", "orgCode": "3", "entAccount": "1", "legalIdCard": "5", "bankDeposit": "9", "linkIdCard": "7", "entBankCardNo": "10", "authTime": "12", "linkName": "8" }

3

放入13个元素,之后删除一个元素,之后再重构这个json对象的顺序 { "legalName": "4", "linkPhone": "6", "entBankPhone": "11", "entName": "2", "orgCode": "3", "legalIdCard": "5", "bankDeposit": "9", "entAccount": "1", "linkIdCard": "7", "entBankCardNo": "10", "authTime": "12", "linkName": "8" }

6

重构代码 /**

把13个参数json对象去除一个,改为12个 @return */ public JSONObject removeFastJson_12(){ // 这个方法就是获取13个参数的json,顺序与12个的一致 JSONObject jsonObject = fastJson_13(); jsonObject.remove("userCategory"); Set<Map.Entry<String, Object>> entries = jsonObject.entrySet(); JSONObject object = new JSONObject(); for (Map.Entry<String, Object> entry : entries) { object.put(entry.getKey(),entry.getValue()); } return object; }
5

完整的测试代码: package com.syb.demo.test_fastjson;

import com.alibaba.fastjson.JSONObject;

import java.util.Map; import java.util.Set;

/**

@author suyibo

@date 2018/5/8 15:49 */ public class TestFastJsonOOM {

private final String entAccount="1"; private final String entName="2"; private final String orgCode="3"; private final String legalName="4"; private final String legalIdCard="5"; private final String linkPhone="6"; private final String linkIdCard="7"; private final String linkName="8"; private final String bankDeposit="9"; private final String entBankCardNo="10"; private final String entBankPhone="11"; private final String authTime="12"; private final String userCategory="13";

public static void main(String[] args) { TestFastJsonOOM testFastJsonOOM = new TestFastJsonOOM(); JSONObject fastJson12 = testFastJsonOOM.fastJson_12(); JSONObject removeFastJson12 = testFastJsonOOM.removeFastJson_12(); System.out.println("-------------12个原生参数-------------"); System.out.println(fastJson12); System.out.println("--------------------------------------"); System.out.println("-------------13个移除一个参数-------------"); System.out.println(removeFastJson12); System.out.println("--------------------------------------");

}

/**

12个参数的json对象 */ public JSONObject fastJson_12(){ JSONObject jsonObject = new JSONObject(); jsonObject.put("entAccount",entAccount); jsonObject.put("entName",entName); jsonObject.put("orgCode",orgCode); jsonObject.put("legalName",legalName); jsonObject.put("legalIdCard",legalIdCard); jsonObject.put("linkPhone",linkPhone); jsonObject.put("linkIdCard",linkIdCard); jsonObject.put("linkName",linkName); jsonObject.put("bankDeposit",bankDeposit); jsonObject.put("entBankCardNo",entBankCardNo); jsonObject.put("entBankPhone",entBankPhone); jsonObject.put("authTime",authTime); return jsonObject; }

/**

13个参数的json对象 @return */ public JSONObject fastJson_13(){ JSONObject jsonObject = fastJson_12(); jsonObject.put("userCategory",userCategory); return jsonObject; }

/**

把13个参数json对象去除一个,改为12个 @return */ public JSONObject removeFastJson_12(){ JSONObject jsonObject = fastJson_13(); jsonObject.remove("userCategory"); Set<Map.Entry<String, Object>> entries = jsonObject.entrySet(); JSONObject object = new JSONObject(); for (Map.Entry<String, Object> entry : entries) { object.put(entry.getKey(),entry.getValue()); } return object; }

}