Android中实现JSON字符串和JSON对象的转换

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

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

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

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值