Java中对jsonArray的排序,使用的是Gson

使用Gson对json解析字符串,转化为json对象.

先上代码: 下面是main方法里面的代码

1package testJava; 2 3import java.util.ArrayList; 4import java.util.Collections; 5import java.util.List; 6 7import com.google.gson.JsonParser; 8import com.google.gson.JsonArray; 9import com.google.gson.JsonObject; 10 11public class TestJava { 12 13 public static void main(String[] args) { 14 // TODO Auto-generated method stub 15 jsoaArraySort(); 16 } 17 public static void jsoaArraySort() { 18 String arrayData = "[" 19 + "{\"Name\":\"TVS\",\"No\":" + 202 + ",\"Length\":" + 23 + "}," 20 + "{\"Name\":\"TVC\",\"No\":" + 203 + ",\"Length\":" + 14 + "}," 21 + "{\"Name\":\"Wel\",\"No\":" + 345 + ",\"Length\":" + 35 + "}," 22 + "{\"Name\":\"Sma\",\"No\":" + 678 + ",\"Length\":" + 45 + "}," 23 + "{\"Name\":\"Sma\",\"No\":" + 136 + ",\"Length\":" + 15 + "}," 24 + "{\"Name\":\"Cus\",\"No\":" + 257 + ",\"Length\":" + 17 + "}," 25 + "{\"Name\":\"And\",\"No\":" + 678 + ",\"Length\":" + 16 + "}," 26 + "{\"Name\":\"Roo\",\"No\":" + 136 + ",\"Length\":" + 56 + "}" 27 +"]"; 28 JsonParser parser = new JsonParser(); 29 JsonArray jsonArray = parser.parse(arrayData).getAsJsonArray(); 30 System.out.println("init jsonArray=" + jsonArray.toString()); 31 32 JsonArray sort_JsonArray = new JsonArray(); 33 List<JsonObject> list = new ArrayList<JsonObject>(); 34 JsonObject jsonObj = null; 35 for (int i = 0; i < jsonArray.size(); i++) { 36 jsonObj = (JsonObject) jsonArray.get(i); 37 list.add(jsonObj); 38 } 39 //这里最核心的地方就是SortComparator这个类 40 //其中构造方法的参数: 41 //sortItem是要排序的jsonArray中一个元素, 这里我选择是Name, 也可以选择No或者是Length 42 //sortType是排序的类型, 有三种情况 43 // 1. 排序的元素对应的值是int, 那么sortType = "int"; 44 // 2. 排序的元素对应的值是string, 那么sortType = "string"; 45 // 3. 排序的元素对应的是是其他类型, 默认是不排序, (后面可以扩展) 46 //sortDire是排序的方向, 可以是asc或者desc, 默认是数据的原始方向(就是没有排序方向) 47 String sortItem = "Length"; 48 String sortType = "int"; 49 String sortDire = "desc"; 50 Collections.sort(list, new SortComparator(sortItem, sortType, sortDire)); 51 for (int i = 0; i < list.size(); i++) { 52 jsonObj = list.get(i); 53 sort_JsonArray.add(jsonObj); 54 } 55 System.out.println("after sort_JsonArray=" + sort_JsonArray.toString()); 56 } 57}

下面给出SortComparator.java

1package testJava; 2 3import java.util.Comparator; 4import com.google.gson.JsonObject; 5 6public class SortComparator implements Comparator<JsonObject> { 7 8 private String sortItem; 9 private String sortType; 10 private String sortDire; 11 12 public SortComparator(String sortItem, String sortType, String sortDire) { 13 this.sortItem = sortItem; 14 this.sortType = sortType; 15 this.sortDire = sortDire; 16 } 17 18 @Override 19 public int compare(JsonObject o1, JsonObject o2) { 20 String value1 = o1.getAsJsonObject().get(sortItem).getAsString(); 21 String value2 = o2.getAsJsonObject().get(sortItem).getAsString(); 22 if ("int".equalsIgnoreCase(this.sortType)) { // int sort 23 int int1 = Integer.parseInt(value1); 24 int int2 = Integer.parseInt(value2); 25 if ("asc".equalsIgnoreCase(this.sortDire)) { 26 return int1 - int2; 27 } else if ("desc".equalsIgnoreCase(this.sortDire)) { 28 return int2 - int1; 29 } else { 30 return 0; 31 } 32 } else if ("string".equalsIgnoreCase(this.sortType)) { // string sort 33 if ("asc".equalsIgnoreCase(this.sortDire)) { 34 return value1.compareTo(value2); 35 } else if ("desc".equalsIgnoreCase(this.sortDire)) { 36 return value2.compareTo(value1); 37 } else { 38 return 0; 39 } 40 } else { // nothing sort 41 return 0; 42 } 43 } 44}

测试的结果:

1jsonArray的初始值如下: 2jsonArray = [ 3  {"Name":"TVS","No":202,"Length":23}, 4  {"Name":"TVC","No":203,"Length":14}, 5  {"Name":"Wel","No":345,"Length":35}, 6  {"Name":"Sma","No":678,"Length":45}, 7  {"Name":"Sma","No":136,"Length":15}, 8  {"Name":"Cus","No":257,"Length":17}, 9  {"Name":"And","No":678,"Length":16}, 10  {"Name":"Roo","No":136,"Length":56} 11]; 12 13下面是按照Name元素从小到达排序: SortComparator("Name", "string", "asc") 14after sort_JsonArray=[ 15   {"Name":"And","No":678,"Length":16}, 16   {"Name":"Cus","No":257,"Length":17}, 17   {"Name":"Roo","No":136,"Length":56}, 18   {"Name":"Sma","No":678,"Length":45}, 19   {"Name":"Sma","No":136,"Length":15}, 20   {"Name":"TVC","No":203,"Length":14}, 21   {"Name":"TVS","No":202,"Length":23}, 22   {"Name":"Wel","No":345,"Length":35} 23 ] 24 25下面是按照Name元素从大到小排序: SortComparator("Name", "string", "desc") 26after sort_JsonArray=[ 27 {"Name":"Wel","No":345,"Length":35}, 28 {"Name":"TVS","No":202,"Length":23}, 29 {"Name":"TVC","No":203,"Length":14}, 30 {"Name":"Sma","No":678,"Length":45}, 31 {"Name":"Sma","No":136,"Length":15}, 32 {"Name":"Roo","No":136,"Length":56}, 33 {"Name":"Cus","No":257,"Length":17}, 34 {"Name":"And","No":678,"Length":16} 35] 36 37下面是按照Length元素从小到大排序: SortComparator("Length", "int", "asc") 38after sort_JsonArray=[ 39 {"Name":"TVC","No":203,"Length":14}, 40 {"Name":"Sma","No":136,"Length":15}, 41 {"Name":"And","No":678,"Length":16}, 42 {"Name":"Cus","No":257,"Length":17}, 43 {"Name":"TVS","No":202,"Length":23}, 44 {"Name":"Wel","No":345,"Length":35}, 45 {"Name":"Sma","No":678,"Length":45}, 46 {"Name":"Roo","No":136,"Length":56} 47] 48 49下面是按照Length元素从大到小排序: SortComparator("Length", "int", "desc") 50after sort_JsonArray=[ 51 {"Name":"Roo","No":136,"Length":56}, 52 {"Name":"Sma","No":678,"Length":45}, 53 {"Name":"Wel","No":345,"Length":35}, 54 {"Name":"TVS","No":202,"Length":23}, 55 {"Name":"Cus","No":257,"Length":17}, 56 {"Name":"And","No":678,"Length":16}, 57 {"Name":"Sma","No":136,"Length":15}, 58 {"Name":"TVC","No":203,"Length":14} 59]
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )