解析json串,并按照某个字段排序
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
本文将为大家介绍json解析并按照某个字段排序的方法。如果是汉字,转化为汉语拼音后,按照字典顺序排序。
原始json
[{"proportionCost":0,"busiName":"aaav","userList":"李雷","allCost":53800,"liuCost":"0","projectName":"AMBHOME","storageCost":52000,"calculationCost":1800},
{"proportionCost":0,"busiName":"aaaa","userList":"韩梅梅","allCost":1000,"liuCost":"0","projectName":"SCCS","storageCost":0,"calculationCost":0},
{"proportionCost":0,"busiName":"aaas","userList":"孙悟空","allCost":3000,"liuCost":"0","projectName":"JPJ","storageCost":0,"calculationCost":1800},
{"proportionCost":0,"busiName":"aaaj","userList":"白骨精","allCost":2600,"liuCost":"0","projectName":"EMAIL","storageCost":2600,"calculationCost":0},
{"proportionCost":0,"busiName":"aaah","userList":"牛魔王","allCost":2000,"liuCost":"0","projectName":"FOP","storageCost":0,"calculationCost":0},
{"proportionCost":0,"busiName":"aaab","userList":"周瑜","allCost":1200,"liuCost":"0","projectName":"TXTP","storageCost":0,"calculationCost":0},
{"proportionCost":0,"busiName":"aaak","userList":"黄盖","allCost":8000,"liuCost":"0","projectName":"FQL","storageCost":0,"calculationCost":0}]
一、value为int,按照数值的大小排序
public void sortByInt(){
String result="[{\"proportionCost\":0,\"busiName\":\"aaav\",\"userList\":\"李雷\",\"allCost\":53800,\"liuCost\":\"0\",\"projectName\":\"AMBHOME\",\"storageCost\":52000,\"calculationCost\":1800},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaaa\",\"userList\":\"韩梅梅\",\"allCost\":1000,\"liuCost\":\"0\",\"projectName\":\"SCCS\",\"storageCost\":0,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaas\",\"userList\":\"孙悟空\",\"allCost\":3000,\"liuCost\":\"0\",\"projectName\":\"JPJ\",\"storageCost\":0,\"calculationCost\":1800},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaaj\",\"userList\":\"白骨精\",\"allCost\":2600,\"liuCost\":\"0\",\"projectName\":\"EMAIL\",\"storageCost\":2600,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaah\",\"userList\":\"牛魔王\",\"allCost\":2000,\"liuCost\":\"0\",\"projectName\":\"FOP\",\"storageCost\":0,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaab\",\"userList\":\"周瑜\",\"allCost\":1200,\"liuCost\":\"0\",\"projectName\":\"TXTP\",\"storageCost\":0,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaak\",\"userList\":\"黄盖\",\"allCost\":8000,\"liuCost\":\"0\",\"projectName\":\"FQL\",\"storageCost\":0,\"calculationCost\":0}]";
System.out.println(result);
JSONArray jsonArray = JSON.parseArray(result);
JSONArray sortJsonArray = new JSONArray();
List<JSONObject> jsonValue = new ArrayList<JSONObject>();
for (int i = 0; i < jsonArray.size(); i++){
jsonValue.add(jsonArray.getJSONObject(i));
}
Collections.sort(jsonValue, new Comparator<JSONObject>() {
private static final String key = "allCost";
@Override
public int compare(JSONObject o1, JSONObject o2) {
int va1 = o1.getIntValue(key);
int va2 = o2.getIntValue(key);
return va1-va2;
}
});
for(int i = 0; i < jsonArray.size(); i++){
sortJsonArray.add(jsonValue.get(i));
}
System.out.println(sortJsonArray.toJSONString());
}
运行结果
[{"proportionCost":0,"busiName":"aaaa","userList":"韩梅梅","allCost":1000,"liuCost":"0","projectName":"SCCS","storageCost":0,"calculationCost":0},{"proportionCost":0,"busiName":"aaab","userList":"周瑜","allCost":1200,"liuCost":"0","projectName":"TXTP","storageCost":0,"calculationCost":0},{"proportionCost":0,"busiName":"aaah","userList":"牛魔王","allCost":2000,"liuCost":"0","projectName":"FOP","storageCost":0,"calculationCost":0},{"proportionCost":0,"busiName":"aaaj","userList":"白骨精","allCost":2600,"liuCost":"0","projectName":"EMAIL","storageCost":2600,"calculationCost":0},{"proportionCost":0,"busiName":"aaas","userList":"孙悟空","allCost":3000,"liuCost":"0","projectName":"JPJ","storageCost":0,"calculationCost":1800},{"proportionCost":0,"busiName":"aaak","userList":"黄盖","allCost":8000,"liuCost":"0","projectName":"FQL","storageCost":0,"calculationCost":0},{"proportionCost":0,"busiName":"aaav","userList":"李雷","allCost":53800,"liuCost":"0","projectName":"AMBHOME","storageCost":52000,"calculationCost":1800}]
二、value为String类型,按照字典顺序排序
public void sortByString(){
String result="[{\"proportionCost\":0,\"busiName\":\"aaav\",\"userList\":\"李雷\",\"allCost\":53800,\"liuCost\":\"0\",\"projectName\":\"AMBHOME\",\"storageCost\":52000,\"calculationCost\":1800},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaaa\",\"userList\":\"韩梅梅\",\"allCost\":1000,\"liuCost\":\"0\",\"projectName\":\"SCCS\",\"storageCost\":0,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaas\",\"userList\":\"孙悟空\",\"allCost\":3000,\"liuCost\":\"0\",\"projectName\":\"JPJ\",\"storageCost\":0,\"calculationCost\":1800},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaaj\",\"userList\":\"白骨精\",\"allCost\":2600,\"liuCost\":\"0\",\"projectName\":\"EMAIL\",\"storageCost\":2600,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaah\",\"userList\":\"牛魔王\",\"allCost\":2000,\"liuCost\":\"0\",\"projectName\":\"FOP\",\"storageCost\":0,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaab\",\"userList\":\"周瑜\",\"allCost\":1200,\"liuCost\":\"0\",\"projectName\":\"TXTP\",\"storageCost\":0,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaak\",\"userList\":\"黄盖\",\"allCost\":8000,\"liuCost\":\"0\",\"projectName\":\"FQL\",\"storageCost\":0,\"calculationCost\":0}]";
System.out.println(result);
JSONArray jsonArray = JSON.parseArray(result);
JSONArray sortJsonArray = new JSONArray();
List<JSONObject> jsonValue = new ArrayList<JSONObject>();
for (int i = 0; i < jsonArray.size(); i++){
jsonValue.add(jsonArray.getJSONObject(i));
}
Collections.sort(jsonValue, new Comparator<JSONObject>() {
private static final String key = "busiName";
@Override
public int compare(JSONObject o1, JSONObject o2) {
String val1 = o1.getString(key);
String val2 = o2.getString(key);
return val1.compareTo(val2);
}
});
for(int i = 0; i < jsonArray.size(); i++){
sortJsonArray.add(jsonValue.get(i));
}
System.out.println(sortJsonArray.toJSONString());
}
运行结果
[{"proportionCost":0,"busiName":"aaaa","userList":"韩梅梅","allCost":1000,"liuCost":"0","projectName":"SCCS","storageCost":0,"calculationCost":0},{"proportionCost":0,"busiName":"aaab","userList":"周瑜","allCost":1200,"liuCost":"0","projectName":"TXTP","storageCost":0,"calculationCost":0},{"proportionCost":0,"busiName":"aaah","userList":"牛魔王","allCost":2000,"liuCost":"0","projectName":"FOP","storageCost":0,"calculationCost":0},{"proportionCost":0,"busiName":"aaaj","userList":"白骨精","allCost":2600,"liuCost":"0","projectName":"EMAIL","storageCost":2600,"calculationCost":0},{"proportionCost":0,"busiName":"aaak","userList":"黄盖","allCost":8000,"liuCost":"0","projectName":"FQL","storageCost":0,"calculationCost":0},{"proportionCost":0,"busiName":"aaas","userList":"孙悟空","allCost":3000,"liuCost":"0","projectName":"JPJ","storageCost":0,"calculationCost":1800},{"proportionCost":0,"busiName":"aaav","userList":"李雷","allCost":53800,"liuCost":"0","projectName":"AMBHOME","storageCost":52000,"calculationCost":1800}]
三、value为汉字,按照汉语拼音的字典顺序排序
首先,需要添加汉字转拼音的工具类的依赖
<!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
/**
* value为汉字,按照汉语拼音的字典顺序排序
*/
@Test
public void sortByPinyin(){
String result="[{\"proportionCost\":0,\"busiName\":\"aaav\",\"userList\":\"李雷\",\"allCost\":53800,\"liuCost\":\"0\",\"projectName\":\"AMBHOME\",\"storageCost\":52000,\"calculationCost\":1800},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaaa\",\"userList\":\"韩梅梅\",\"allCost\":1000,\"liuCost\":\"0\",\"projectName\":\"SCCS\",\"storageCost\":0,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaas\",\"userList\":\"孙悟空\",\"allCost\":3000,\"liuCost\":\"0\",\"projectName\":\"JPJ\",\"storageCost\":0,\"calculationCost\":1800},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaaj\",\"userList\":\"白骨精\",\"allCost\":2600,\"liuCost\":\"0\",\"projectName\":\"EMAIL\",\"storageCost\":2600,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaah\",\"userList\":\"牛魔王\",\"allCost\":2000,\"liuCost\":\"0\",\"projectName\":\"FOP\",\"storageCost\":0,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaab\",\"userList\":\"周瑜\",\"allCost\":1200,\"liuCost\":\"0\",\"projectName\":\"TXTP\",\"storageCost\":0,\"calculationCost\":0},\n" +
"{\"proportionCost\":0,\"busiName\":\"aaak\",\"userList\":\"黄盖\",\"allCost\":8000,\"liuCost\":\"0\",\"projectName\":\"FQL\",\"storageCost\":0,\"calculationCost\":0}]";
System.out.println(result);
JSONArray jsonArray = JSON.parseArray(result);
JSONArray sortJsonArray = new JSONArray();
List<JSONObject> jsonValue = new ArrayList<JSONObject>();
for (int i = 0; i < jsonArray.size(); i++){
jsonValue.add(jsonArray.getJSONObject(i));
}
Collections.sort(jsonValue, new Comparator<JSONObject>() {
private static final String key = "userList";
@Override
public int compare(JSONObject o1, JSONObject o2) {
String val1 = getPinyin(o1.getString(key));
String val2 = getPinyin(o2.getString(key));
return val1.compareTo(val2);
}
});
for(int i = 0; i < jsonArray.size(); i++){
sortJsonArray.add(jsonValue.get(i));
}
System.out.println(sortJsonArray.toJSONString());
}
/**
* 汉字转拼音
* @param chinese
* @return
*/
public static String getPinyin(String chinese){
String pinyinStr = "";
char[] newChar = chinese.toCharArray();
HanyuPinyinOutputFormat pinyinFormat = new HanyuPinyinOutputFormat();
pinyinFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
pinyinFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for(int i = 0; i < newChar.length; i++){
if(newChar[i] > 128){
try {
pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], pinyinFormat)[0];
// pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], pinyinFormat)[0];//转为拼音后取首字母
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
}else{
pinyinStr += newChar[i];
}
}
return pinyinStr;
}
运行结果
[{"proportionCost":0,"busiName":"aaaj","userList":"白骨精","allCost":2600,"liuCost":"0","projectName":"EMAIL","storageCost":2600,"calculationCost":0},{"proportionCost":0,"busiName":"aaaa","userList":"韩梅梅","allCost":1000,"liuCost":"0","projectName":"SCCS","storageCost":0,"calculationCost":0},{"proportionCost":0,"busiName":"aaak","userList":"黄盖","allCost":8000,"liuCost":"0","projectName":"FQL","storageCost":0,"calculationCost":0},{"proportionCost":0,"busiName":"aaav","userList":"李雷","allCost":53800,"liuCost":"0","projectName":"AMBHOME","storageCost":52000,"calculationCost":1800},{"proportionCost":0,"busiName":"aaah","userList":"牛魔王","allCost":2000,"liuCost":"0","projectName":"FOP","storageCost":0,"calculationCost":0},{"proportionCost":0,"busiName":"aaas","userList":"孙悟空","allCost":3000,"liuCost":"0","projectName":"JPJ","storageCost":0,"calculationCost":1800},{"proportionCost":0,"busiName":"aaab","userList":"周瑜","allCost":1200,"liuCost":"0","projectName":"TXTP","storageCost":0,"calculationCost":0}]
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
4 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献2条内容
所有评论(0)