Android开发:Json字符串到Json对象万能解析器

移动开发 Android
本文介绍了如何用Java实现Json字符串到Json对象万能解析器,即通过CommonJSONParser可以把json字符串转换为包含Map、List、String、Integer等标准Java对象的集合。

json字符串到json对象万能转换器(java实现),就一百来行代码,非常轻量小巧。对于一般应用场景资源消耗非常低,速度也足够快,尤其适用于Android应用开发。

通过CommonJSONParser可以把json字符串转换为包含Map、List、String、Integer等标准Java对象的集合,具体使用方法:

  CommonJSONParser commonJSONParser = new CommonJSONParser();
  Map<String, Object> result = commonJSONParser.parse(jsonDataStr);

CommonJSONParser源代码如下(主要使用“递归”思想):

  1. 1 import java.util.ArrayList; 
  2.  2 import java.util.HashMap; 
  3.  3 import java.util.Iterator; 
  4.  4 import java.util.List; 
  5.  5 import java.util.Map; 
  6.  6  
  7.  7 import org.json.JSONArray; 
  8.  8 import org.json.JSONException; 
  9.  9 import org.json.JSONObject; 
  10. 10  
  11. 11 public class CommonJSONParser { 
  12. 12  
  13. 13     public Map<String, Object> parse(String jsonStr) { 
  14. 14  
  15. 15         Map<String, Object> result = null
  16. 16  
  17. 17         if (null != jsonStr) { 
  18. 18             try { 
  19. 19  
  20. 20                 JSONObject jsonObject = new JSONObject(jsonStr); 
  21. 21                 result = parseJSONObject(jsonObject); 
  22. 22  
  23. 23             } catch (JSONException e) { 
  24. 24                 // TODO Auto-generated catch block 
  25. 25                 e.printStackTrace(); 
  26. 26             } 
  27. 27         } // if (null != jsonStr) 
  28. 28  
  29. 29         return result; 
  30. 30     } 
  31. 31  
  32. 32     private Object parseValue(Object inputObject) throws JSONException { 
  33. 33         Object outputObject = null
  34. 34  
  35. 35         if (null != inputObject) { 
  36. 36  
  37. 37             if (inputObject instanceof JSONArray) { 
  38. 38                 outputObject = parseJSONArray((JSONArray) inputObject); 
  39. 39             } else if (inputObject instanceof JSONObject) { 
  40. 40                 outputObject = parseJSONObject((JSONObject) inputObject); 
  41. 41             } else if (inputObject instanceof String || inputObject instanceof Boolean || inputObject instanceof Integer) { 
  42. 42                 outputObject = inputObject; 
  43. 43             } 
  44. 44  
  45. 45         } 
  46. 46  
  47. 47         return outputObject; 
  48. 48     } 
  49. 49  
  50. 50     private List<Object> parseJSONArray(JSONArray jsonArray) throws JSONException { 
  51. 51  
  52. 52         List<Object> valueList = null
  53. 53  
  54. 54         if (null != jsonArray) { 
  55. 55             valueList = new ArrayList<Object>(); 
  56. 56  
  57. 57             for (int i = 0; i < jsonArray.length(); i++) { 
  58. 58                 Object itemObject = jsonArray.get(i); 
  59. 59                 if (null != itemObject) { 
  60. 60                     valueList.add(parseValue(itemObject)); 
  61. 61                 } 
  62. 62             } // for (int i = 0; i < jsonArray.length(); i++) 
  63. 63         } // if (null != valueStr) 
  64. 64  
  65. 65         return valueList; 
  66. 66     } 
  67. 67  
  68. 68     private Map<String, Object> parseJSONObject(JSONObject jsonObject) throws JSONException { 
  69. 69  
  70. 70         Map<String, Object> valueObject = null
  71. 71         if (null != jsonObject) { 
  72. 72             valueObject = new HashMap<String, Object>(); 
  73. 73  
  74. 74             Iterator<String> keyIter = jsonObject.keys(); 
  75. 75             while (keyIter.hasNext()) { 
  76. 76                 String keyStr = keyIter.next(); 
  77. 77                 Object itemObject = jsonObject.opt(keyStr); 
  78. 78                 if (null != itemObject) { 
  79. 79                     valueObject.put(keyStr, parseValue(itemObject)); 
  80. 80                 } // if (null != itemValueStr) 
  81. 81  
  82. 82             } // while (keyIter.hasNext()) 
  83. 83         } // if (null != valueStr) 
  84. 84  
  85. 85         return valueObject; 
  86. 86     } 
  87. 87 } 

 

责任编辑:闫佳明 来源: cnblogs
相关推荐

2020-12-02 10:13:45

JacksonJDK解析器

2022-02-14 13:58:32

操作系统JSON格式鸿蒙

2010-01-07 16:55:06

JSON字符串

2015-07-02 10:37:32

C#Json字符串类代码

2023-05-05 07:49:07

GolangJSON格式

2009-03-23 14:14:33

JSONAJAXJavaScript

2013-02-20 15:29:00

JSONAndroid开发

2010-01-07 16:37:04

JSON解析器

2010-01-07 17:24:31

JSON 解析器

2017-03-21 07:54:43

解码器软件程序

2022-12-22 14:56:44

2022-06-28 08:17:10

JSON性能反射

2023-12-30 13:33:36

Python解析器JSON

2016-11-24 12:07:42

Android万能圆角ImageView

2024-02-19 15:38:08

JsonPython字符串

2010-01-05 16:48:16

JSON 字符串

2010-01-06 13:32:27

JSON数据

2010-07-06 10:07:10

jQueryJSON

2024-02-22 08:06:45

JSON策略解析器

2010-01-07 15:52:23

JSON字符串
点赞
收藏

51CTO技术栈公众号