Java代码中如何在JSONObject按put顺序排序
编写java代码时,发现在JSONObject 对象中put 一些数据后,输出JSONObject对象信息时,展示的json信息中,排序发生了变化。
以下为部分代码:
JSONObject expDataJson = new JSONObject();
JSONObject expJson = new JSONObject();
JSONObject quJson = new JSONObject();
JSONObject solrJson = new JSONObject();
JSONObject rankingJson = new JSONObject();
JSONObject filterJson = new JSONObject();
JSONObject actuatorJson = new JSONObject();
expDataJson.put("expName",expName);
expDataJson.put("errorRate",round(errorRate,3));
//设置expJson
expJson.put("avgTime",round(expAvgTime, 3));
expJson.put("95Time",round(exp95Time, 3));
expJson.put("99Time",round(exp99Time, 3));
expJson.put("99TimeThreshold",round(expectExp99Time, 3));
//设置quJson
quJson.put("99Time",round(qu99Time, 3));
quJson.put("cpu",round(quCpuValue, 3));
quJson.put("cpuThreshold",0.6f);
//设置solrJson
solrJson.put("99Time",round(solr99Time, 3));
solrJson.put("cpu",round(solrCpuValue, 3));
solrJson.put("cpuThreshold",0.15f);
//设置rankingJson
rankingJson.put("ranking_72_99Time",round(ranking_72_99Time,3));
rankingJson.put("ranking_71_99Time", round(ranking_71_99Time,3));
rankingJson.put("ranking_73_99Time",round(ranking_73_99Time,3));
rankingJson.put("errorRate",round(errorRate,3));
rankingJson.put("cpu",round(rankingCpuValue,3));
rankingJson.put("cpuThreshold",0.6f);
//filterJson
filterJson.put("filter99Time",round(filter99Time,3));
//actuatorJson
actuatorJson.put("cpu",round(actuatorCpuValue,3));
actuatorJson.put("cpuThreshold",0.6f);
expDataJson.put("expTime",expJson);
expDataJson.put("qu",quJson);
expDataJson.put("solr",solrJson);
expDataJson.put("ranking",rankingJson);
expDataJson.put("filter",filterJson);
expDataJson.put("actuator",actuatorJson);
reportJson.put("data",expDataJson);
输出结果:
{
"result": "sucess",
"code": 200,
"data": {
"filter": {
"filter99Time": 0.049
},
"expName": "aaf334f2d083c17702cb0d3bf7e728b6",
"actuator": {
"cpuThreshold": 0.6,
"cpu": 0.142
},
"qu": {
"99Time": 0.074,
"cpuThreshold": 0.6,
"cpu": 0.026
},
"expTime": {
"99Time": 0.69,
"avgTime": 0.335,
"99TimeThreshold": 0.7,
"95Time": 0.514
},
"solr": {
"99Time": 0.238,
"cpuThreshold": 0.15,
"cpu": 0.025
},
"ranking": {
"ranking_71_99Time": 0.126,
"ranking_72_99Time": 0.154,
"cpuThreshold": 0.6,
"cpu": 0.143,
"errorRate": 0,
"ranking_73_99Time": 0
},
"errorRate": 0
},
"message": "压测通过"
}
发现data下的对象顺序乱了。
原因:JsonObject内部是用Hashmap来存储的,所以输出是按key的排序来的,如果要让JsonObject按固定顺序(put的顺序)排列,可以修改JsonObject的定义HashMap改为LinkedHashMap。
修改后部分代码:
JSONObject expDataJson = new JSONObject(new LinkedHashMap());
JSONObject expJson = new JSONObject(new LinkedHashMap());
JSONObject quJson =new JSONObject(new LinkedHashMap());
JSONObject solrJson = new JSONObject(new LinkedHashMap());
JSONObject rankingJson = new JSONObject(new LinkedHashMap());
JSONObject filterJson = new JSONObject(new LinkedHashMap());
JSONObject actuatorJson = new JSONObject(new LinkedHashMap());
输出正常结果:
{
"result": "sucess",
"code": 200,
"data": {
"expName": "aaf334f2d083c17702cb0d3bf7e728xx",
"errorRate": 0,
"expTime": {
"avgTime": 0.335,
"95Time": 0.514,
"99Time": 0.69,
"99TimeThreshold": 0.7
},
"qu": {
"99Time": 0.074,
"cpu": 0.026,
"cpuThreshold": 0.6
},
"solr": {
"99Time": 0.238,
"cpu": 0.025,
"cpuThreshold": 0.15
},
"ranking": {
"ranking_72_99Time": 0.154,
"ranking_71_99Time": 0.126,
"ranking_73_99Time": 0,
"errorRate": 0,
"cpu": 0.143,
"cpuThreshold": 0.6
},
"filter": {
"filter99Time": 0.049
},
"actuator": {
"cpu": 0.142,
"cpuThreshold": 0.6
}
},
"message": "压测通过"
}
更多推荐
所有评论(0)