使用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]