編輯:Android資訊
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 }
FaceBook推出的Android圖片加載庫Fresco
在Android設備上面,快速高效的顯示圖片是極為重要的。過去的幾年裡,我們在如何高效的存儲圖像這方面遇到了很多問題。圖片太大,但是手機的內存卻很小。每一個像素的
Android項目的依賴關系解析
在Android SDK Tools和Eclipse ADT 插件的第17版本 (revision 17)中,我們對Android項目的依賴關系管理做了很多改變。
Android Gesture 手勢創建以及使用示例
在Android1.6的模擬器裡面預裝了一個叫Gestures Builder的程序,這個程序就是讓你創建自己的手勢的(Gestures Builder的源代碼在
Android使用Fragment打造萬能頁面切換框架
首先我們來回憶一下傳統用Activity進行的頁面切換,activity之間切換,首先需要新建intent對象,給該對象設置一些必須的參數,然後調用startAc