gson 用于排序和选择的Java应用程序

jaql4c8m  于 2022-11-06  发布在  Java
关注(0)|答案(1)|浏览(178)

我需要开发一个简单的应用程序,根据预定义的规则对数据进行排序和选择。该应用程序必须能够处理任意结构的对象的JSON列表,选择包含具有特定值的键的对象,并且还可以使用自然排序顺序按值对对象进行排序。
我创建了一个应用程序,它使用“include”规则来接受一组键:值对,以检查条目是否匹配。

  1. static class DataJSON{
  2. public condition condition;
  3. public ArrayList<data> data;
  4. public class exclude{
  5. public String name;
  6. public Integer rating;
  7. public Boolean disabled;
  8. public String user;
  9. }
  10. public class include{
  11. public String name;
  12. public Integer rating;
  13. public Boolean disabled;
  14. public String user;
  15. }
  16. public class data{
  17. public Boolean disabled;
  18. public Integer rating;
  19. public String user;
  20. public String name;
  21. }
  22. public class condition{
  23. public ArrayList<exclude> exclude;
  24. public ArrayList<include> include;
  25. public ArrayList<String> sort_by;
  26. }
  27. }
  28. static class resultPrepare{
  29. public Boolean disabled;
  30. public Integer rating;
  31. public String user;
  32. public String name;
  33. public resultPrepare(Boolean disabled, Integer rating, String user, String name){
  34. this.disabled = disabled;
  35. this.rating = rating;
  36. this.user = user;
  37. this.name = name;
  38. }
  39. }
  40. static class DataResult{
  41. //public Test.result result;
  42. public static ArrayList<result> result;
  43. public DataResult(ArrayList<result> result){
  44. this.result = result;
  45. }
  46. }
  47. public static class result{
  48. public Boolean disabled;
  49. public Integer rating;
  50. public String user;
  51. public String name;
  52. public result(Boolean disabled, Integer rating, String user, String name){
  53. this.disabled = disabled;
  54. this.rating = rating;
  55. this.user = user;
  56. this.name = name;
  57. }
  58. }
  59. public static void main(String[] args){
  60. String json = "{\"data\": [{\"user\": \"mike@mail.com\", \"rating\": 20, \"disabled\": false, \"name\": \"Ton1\"},\n" +
  61. "{\"user\": \"greg@mail.com\", \"rating\": 14, \"disabled\": false, \"name\": \"Ton\"},\n" +
  62. "{\"user\": \"john@mail.com\", \"rating\": 25, \"disabled\": false, \"name\": \"T\"}],\n" +
  63. "\"condition\": {\"include\": [{\"name\": \"Ton1\"}],\"exclude\": [{\"disabled\": false, \"name\": \"Tonf\"},{\"disabled\": false, \"name\": \"T\"}], \"sort_by\": [\"rating\"]}}";
  64. Gson gson = new Gson();
  65. DataJSON jsParse = gson.fromJson(json, DataJSON.class);
  66. ArrayList<result> res = new ArrayList<result>();
  67. for(int i = 0; i < jsParse.data.size(); i++){
  68. Boolean flagInclude = false;
  69. Boolean flagExclude = false;
  70. Boolean flagExcludeDisabled = false;
  71. if(jsParse.condition.include != null){
  72. for(int j = 0; j < jsParse.condition.include.size(); j++){
  73. if(jsParse.data.get(i).name != null && jsParse.condition.include.get(j).name != null){
  74. if(jsParse.data.get(i).name.equalsIgnoreCase(jsParse.condition.include.get(j).name) || jsParse.condition.include.get(j).name == null){
  75. if(jsParse.data.get(i).disabled == jsParse.condition.include.get(j).disabled || jsParse.condition.include.get(j).disabled == null){
  76. if(jsParse.data.get(i).rating == jsParse.condition.include.get(j).rating || jsParse.condition.include.get(j).rating == null){
  77. if((jsParse.data.get(i).user!= null && jsParse.condition.include.get(j).user != null)|| jsParse.condition.include.get(j).user == null){
  78. if(jsParse.data.get(i).user.equalsIgnoreCase(jsParse.condition.include.get(j).user) || jsParse.condition.include.get(j).user == null){
  79. flagInclude = true;
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }else{
  89. flagInclude = true;
  90. }
  91. if(jsParse.condition.exclude != null){
  92. for(int j = 0; j < jsParse.condition.exclude.size(); j++){
  93. if(jsParse.data.get(i).name != null && jsParse.condition.exclude.get(j).name != null){
  94. if(jsParse.data.get(i).name.equalsIgnoreCase(jsParse.condition.exclude.get(j).name) || jsParse.condition.exclude.get(j).name == null){
  95. if(jsParse.data.get(i).disabled == jsParse.condition.exclude.get(j).disabled || jsParse.condition.exclude.get(j).disabled == null){
  96. if(jsParse.data.get(i).rating == jsParse.condition.exclude.get(j).rating || jsParse.condition.exclude.get(j).rating == null){
  97. if((jsParse.data.get(i).user!= null && jsParse.condition.exclude.get(j).user != null)|| jsParse.condition.exclude.get(j).user == null){
  98. if(jsParse.data.get(i).user.equalsIgnoreCase(jsParse.condition.exclude.get(j).user) || jsParse.condition.exclude.get(j).user == null){
  99. flagExclude = true;
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }else{
  109. flagExclude = false;
  110. }
  111. System.out.println("flagInclude: " + flagInclude + " flagExclude: " + flagExclude);
  112. if(flagInclude == true && flagExclude == false){
  113. result r = new result( jsParse.data.get(i).disabled, jsParse.data.get(i).rating , jsParse.data.get(i).user, jsParse.data.get(i).name );
  114. res.add(r);
  115. }
  116. }
  117. DataResult dataResult = new DataResult(res);
  118. String jsParse2 = gson.toJson(dataResult.result);
  119. String outJson = "{\"result\":" + jsParse2 + "}";
  120. System.out.println(outJson);
  121. }
  122. }

输出:“结果”:[{“禁用”:假,“评级”:20,“用户”:“mike@mail.com“,“名称”:“Ton 1”}]}

wtzytmuj

wtzytmuj1#

查看java Comparator 接口和 List 接口的 sort 方法。通过它们,您可以为您的'result'类创建自定义比较器(顺便说一句,类名应该始终以大写字母开头)。
您的案例的名称比较器示例。您将需要实现缺少的比较器,并根据您的规则集选择要使用的比较器。

  1. // Add me somewhere in your code
  2. public static class NameComparator implements Comparator<Result>
  3. {
  4. @Override
  5. public int compare(Result o1, Result o2) {
  6. return o1.name.compareTo(o2.name);
  7. }
  8. }
  9. // And in the block where you output the json structure:
  10. DataResult dataResult = new DataResult(res);
  11. DataResult.result.sort(new NameComparator()); // <-- I am new
  12. String jsParse2 = gson.toJson(dataResult.result);
  13. String outJson = "{\"result\":" + jsParse2 + "}";
  14. System.out.println(outJson);

希望这对你有帮助。

展开查看全部

相关问题