json字串到json對象萬能轉換器(java實現),就一百來行代碼,非常輕量小巧。對於一般應用情境資源消耗非常低,速度也足夠快,尤其適用於Android應用開發。
通過CommonJSONParser可以把json字串轉換為包含Map、List、String、Integer等標準Java對象的集合,具體使用方法:
CommonJSONParser commonJSONParser = new CommonJSONParser();
Map<String, Object> result = commonJSONParser.parse(jsonDataStr);
CommonJSONParser原始碼如下(主要使用“遞迴”思想):
Map<String, Object> Map<String, Object> result = ( != JSONObject jsonObject = result = } } Object parseValue(Object inputObject) Object outputObject = ( != (inputObject outputObject = } (inputObject outputObject = } (inputObject String || inputObject Boolean || inputObject outputObject = List<Object> parseJSONArray(JSONArray jsonArray) List<Object> valueList = ( != valueList = ArrayList<Object> ( i = 0; i < jsonArray.length(); i++ Object itemObject = ( != } } Map<String, Object> parseJSONObject(JSONObject jsonObject) Map<String, Object> valueObject = ( != valueObject = HashMap<String, Object> Iterator<String> keyIter = String keyStr = Object itemObject = ( != } } } }