Android development: Json string to Json object universal parser

Source: Internet
Author: User
Tags string to json

Json string to the json object universal converter java Implementation), it is one hundred lines of code, very lightweight and small. For general application scenarios, resource consumption is very low and the speed is fast enough, especially for Android application development.

CommonJSONParser can be used to convert a json String to a set of standard Java objects, such as Map, List, String, and Integer. Usage:

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

The source code of CommonJSONParser mainly uses the "recursion" idea ):

 
 
  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 } 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.