/Basic/app/src/main/java/com/basic/utils/JsonUtil.java

https://gitlab.com/yubaokang/Basic · Java · 119 lines · 82 code · 11 blank · 26 comment · 5 complexity · c96a31a8a419530dc15a56ca46f348b9 MD5 · raw file

  1. package com.basic.utils;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import android.text.TextUtils;
  7. import com.alibaba.fastjson.JSON;
  8. import com.alibaba.fastjson.TypeReference;
  9. /**
  10. * 完成对json数据的解析
  11. */
  12. public class JsonUtil {
  13. /**
  14. * 使用JSON工具把数据转换成json对象
  15. *
  16. * @param value 是对解析的集合的类型
  17. */
  18. public static String toString(Object value) {
  19. String str = JSON.toJSONString(value);
  20. return str;
  21. }
  22. /**
  23. * 对单个javabean进行解析
  24. */
  25. public static <T> T getObj(String json, Class<T> cls) {
  26. T t = null;
  27. try {
  28. t = JSON.parseObject(json, cls);
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. return t;
  33. }
  34. /**
  35. * 对list类型进行解析
  36. */
  37. public static <T> List<T> getListObj(String json, Class<T> cls) {
  38. List<T> list = new ArrayList<T>();
  39. if (!TextUtils.isEmpty(json)) {
  40. try {
  41. list = JSON.parseArray(json, cls);
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. return list;
  47. }
  48. /**
  49. * 对MapString类型数据进行解析
  50. */
  51. public static Map<String, String> getMapStr(String json) {
  52. Map<String, String> map = new HashMap<>();
  53. if (!TextUtils.isEmpty(json)) {
  54. try {
  55. map = JSON.parseObject(json, new TypeReference<Map<String, String>>() {
  56. });
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. return map;
  62. }
  63. /**
  64. * 对MapObject类型数据进行解析
  65. */
  66. public static Map<String, Object> getMapObj(String json) {
  67. Map<String, Object> map = new HashMap<>();
  68. if (!TextUtils.isEmpty(json)) {
  69. try {
  70. map = JSON.parseObject(json, new TypeReference<Map<String, Object>>() {
  71. });
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. return map;
  77. }
  78. /**
  79. * 对list map obj类型进行解析
  80. */
  81. public static List<Map<String, Object>> getListMap(String json) {
  82. List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  83. if (!TextUtils.isEmpty(json)) {
  84. try {
  85. list = JSON.parseObject(json, new TypeReference<List<Map<String, Object>>>() {
  86. });
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. return list;
  92. }
  93. /**
  94. * 对list map String类型进行解析
  95. */
  96. public static List<Map<String, String>> getListMapStr(String json) {
  97. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  98. if (!TextUtils.isEmpty(json)) {
  99. try {
  100. list = JSON.parseObject(json, new TypeReference<List<Map<String, String>>>() {
  101. });
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. return list;
  107. }
  108. }