PageRenderTime 263ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/financialnet/util/JsonUtil.java

https://gitlab.com/huangjunbin/pupu
Java | 80 lines | 68 code | 12 blank | 0 comment | 0 complexity | 307c4ad8f7bf60e548b123d2e9197f78 MD5 | raw file
  1. package com.financialnet.util;
  2. import java.util.List;
  3. import org.json.JSONException;
  4. import org.json.JSONObject;
  5. import android.util.Log;
  6. import com.alibaba.fastjson.JSON;
  7. import com.alibaba.fastjson.serializer.SerializerFeature;
  8. public final class JsonUtil {
  9. private static String TAG = "FastJson";
  10. public static boolean isSuccess(String jsonString) {
  11. JSONObject json;
  12. boolean flag = false;
  13. try {
  14. json = new JSONObject(jsonString);
  15. flag = json.getBoolean("result");
  16. } catch (JSONException e) {
  17. e.printStackTrace();
  18. }
  19. return flag;
  20. }
  21. public static String getArrayString(String jsonString, String key) {
  22. String value = "";
  23. JSONObject json;
  24. try {
  25. json = new JSONObject(jsonString);
  26. value = json.getJSONArray(key).toString();
  27. } catch (JSONException e) {
  28. e.printStackTrace();
  29. }
  30. return value;
  31. }
  32. public static String convertObjectToJson(Object o) {
  33. SerializerFeature[] features = { SerializerFeature.QuoteFieldNames,
  34. SerializerFeature.WriteMapNullValue,
  35. SerializerFeature.WriteNullListAsEmpty,
  36. SerializerFeature.WriteNullStringAsEmpty,
  37. SerializerFeature.WriteNullNumberAsZero,
  38. SerializerFeature.WriteNullBooleanAsFalse,
  39. SerializerFeature.WriteSlashAsSpecial,
  40. SerializerFeature.BrowserCompatible,
  41. SerializerFeature.DisableCircularReferenceDetect,
  42. SerializerFeature.WriteDateUseDateFormat };
  43. try {
  44. Log.d(TAG, JSON.toJSONString(o, features));
  45. return JSON.toJSONString(o, features);
  46. } catch (Exception e) {
  47. Log.e(TAG, e.toString());
  48. return "";
  49. }
  50. }
  51. public static <T> T convertJsonToObject(String json, Class<T> clazz) {
  52. try {
  53. return JSON.parseObject(json, clazz);
  54. } catch (Exception e) {
  55. Log.e(TAG, e.toString());
  56. Log.e("merror", e.toString());
  57. return null;
  58. }
  59. }
  60. public static <T> List<T> convertJsonToList(String json, Class<T> clazz) {
  61. try {
  62. return JSON.parseArray(json, clazz);
  63. } catch (Exception e) {
  64. Log.e(TAG, e.toString());
  65. return null;
  66. }
  67. }
  68. }