码迷,mamicode.com
首页 > Web开发 > 详细

JSON的三种解析方式

时间:2015-04-20 00:19:24      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

一、什么是JSON?

JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度。

JSON就是一串字符串 只不过元素会使用特定的符号标注。

{} 双括号表示对象

[] 中括号表示数组

"" 双引号内是属性或值

: 冒号表示后者是前者的值(这个值可以是字符串、数字、也可以是另一个数组或对象)

所以 {"name": "Michael"} 可以理解为是一个包含name为Michael的对象

而[{"name": "Michael"},{"name": "Jerry"}]就表示包含两个对象的数组

当然了,你也可以使用{"name":["Michael","Jerry"]}来简化上面一部,这是一个拥有一个name数组的对象

二、JSON解析之传统的JSON解析

1、生成JSOn字符串

1 public static String createJsonString(String key, Object value) {
2         JSONObject jsonObject = new JSONObject();
3         jsonObject.put(key, value);
4         return jsonObject.toString();
5     }

2、解析JSON字符串

分为以下三种情况,一个JavaBean,一个List数组,一个嵌套Map的List数组:

 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.JSONObject;
 9  
10 import com.android.myjson.domain.Person;
11  
12 /**
13  * 完成对json数据的解析
14  *
15  */
16 public class JsonTools {
17  
18  
19     public static Person getPerson(String key, String jsonString) {
20         Person person = new Person();
21         try {
22             JSONObject jsonObject = new JSONObject(jsonString);
23             JSONObject personObject = jsonObject.getJSONObject("person");
24             person.setId(personObject.getInt("id"));
25             person.setName(personObject.getString("name"));
26             person.setAddress(personObject.getString("address"));
27         } catch (Exception e) {
28             // TODO: handle exception
29         }
30         return person;
31     }
32  
33     public static List<person> getPersons(String key, String jsonString) {
34         List<person> list = new ArrayList<person>();
35         try {
36             JSONObject jsonObject = new JSONObject(jsonString);
37             // 返回json的数组
38             JSONArray jsonArray = jsonObject.getJSONArray(key);
39             for (int i = 0; i < jsonArray.length(); i++) {
40                 JSONObject jsonObject2 = jsonArray.getJSONObject(i);
41                 Person person = new Person();
42                 person.setId(jsonObject2.getInt("id"));
43                 person.setName(jsonObject2.getString("name"));
44                 person.setAddress(jsonObject2.getString("address"));
45                 list.add(person);
46             }
47         } catch (Exception e) {
48             // TODO: handle exception
49         }
50         return list;
51     }
52  
53     public static List<string> getList(String key, String jsonString) {
54         List<string> list = new ArrayList<string>();
55         try {
56             JSONObject jsonObject = new JSONObject(jsonString);
57             JSONArray jsonArray = jsonObject.getJSONArray(key);
58             for (int i = 0; i < jsonArray.length(); i++) {
59                 String msg = jsonArray.getString(i);
60                 list.add(msg);
61             }
62         } catch (Exception e) {
63             // TODO: handle exception
64         }
65         return list;
66     }
67  
68     public static List<map<string, object="">> listKeyMaps(String key,
69             String jsonString) {
70         List<map<string, object="">> list = new ArrayList<map<string, object="">>();
71         try {
72             JSONObject jsonObject = new JSONObject(jsonString);
73             JSONArray jsonArray = jsonObject.getJSONArray(key);
74             for (int i = 0; i < jsonArray.length(); i++) {
75                 JSONObject jsonObject2 = jsonArray.getJSONObject(i);
76                 Map<string, object=""> map = new HashMap<string, object="">();
77                 Iterator<string> iterator = jsonObject2.keys();
78                 while (iterator.hasNext()) {
79                     String json_key = iterator.next();
80                     Object json_value = jsonObject2.get(json_key);
81                     if (json_value == null) {
82                         json_value = "";
83                     }
84                     map.put(json_key, json_value);
85                 }
86                 list.add(map);
87             }
88         } catch (Exception e) {
89             // TODO: handle exception
90         }
91         return list;
92     }
93 }</string></string,></string,></map<string,></map<string,></map<string,></string></string></string></person></person></person>

 


三、JSON解析之GSON

1、生成JSON字符串

 1 import com.google.gson.Gson;
 2  
 3 public class JsonUtils {
 4  
 5     public static String createJsonObject(Object obj) {
 6         Gson gson = new Gson();
 7         String str = gson.toJson(obj);
 8         return str;
 9  
10     }
11 }

 

2、解析JSON

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import java.util.Map;
 4  
 5 import com.google.gson.Gson;
 6 import com.google.gson.reflect.TypeToken;
 7  
 8 ;
 9 public class GsonTools {
10  
11     public GsonTools() {
12         // TODO Auto-generated constructor stub
13     }
14  
15     /**
16      * @param <t>
17      * @param jsonString
18      * @param cls
19      * @return
20      */
21     public static <t> T getPerson(String jsonString, Class<t> cls) {
22         T t = null;
23         try {
24             Gson gson = new Gson();
25             t = gson.fromJson(jsonString, cls);
26         } catch (Exception e) {
27             // TODO: handle exception
28         }
29         return t;
30     }
31  
32     /**
33      * 使用Gson进行解析 List<person>
34      * 
35      * @param <t>
36      * @param jsonString
37      * @param cls
38      * @return
39      */
40     public static <t> List<t> getPersons(String jsonString, Class<t> cls) {
41         List<t> list = new ArrayList<t>();
42         try {
43             Gson gson = new Gson();
44             list = gson.fromJson(jsonString, new TypeToken<list<t>>() {
45             }.getType());
46         } catch (Exception e) {
47         }
48         return list;
49     }
50  
51     /**
52      * @param jsonString
53      * @return
54      */
55     public static List<string> getList(String jsonString) {
56         List<string> list = new ArrayList<string>();
57         try {
58             Gson gson = new Gson();
59             list = gson.fromJson(jsonString, new TypeToken<list<string>>() {
60             }.getType());
61         } catch (Exception e) {
62             // TODO: handle exception
63         }
64         return list;
65     }
66  
67     public static List<map<string, object="">> listKeyMaps(String jsonString) {
68         List<map<string, object="">> list = new ArrayList<map<string, object="">>();
69         try {
70             Gson gson = new Gson();
71             list = gson.fromJson(jsonString,
72                     new TypeToken<list<map<string, object="">>>() {
73                     }.getType());
74         } catch (Exception e) {
75             // TODO: handle exception
76         }
77         return list;
78     }
79 }
80 </list<map<string,></map<string,></map<string,></map<string,></list<string></string></string></string></list<t></t></t></t></t></t></t></person></t></t></t>

 

 

三、JSON解析之FastJSON

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import java.util.Map;
 4  
 5 import com.alibaba.fastjson.JSON;
 6 import com.alibaba.fastjson.TypeReference;
 7  
 8 public class JsonTool {
 9  
10     public static <t> T getPerson(String jsonstring, Class<t> cls) {
11         T t = null;
12         try {
13             t = JSON.parseObject(jsonstring, cls);
14         } catch (Exception e) {
15             // TODO: handle exception
16         }
17         return t;
18     }
19  
20     public static <t> List<t> getPersonList(String jsonstring, Class<t> cls) {
21         List<t> list = new ArrayList<t>();
22         try {
23             list = JSON.parseArray(jsonstring, cls);
24         } catch (Exception e) {
25             // TODO: handle exception
26         }
27         return list;
28     }
29  
30     public static <t> List<map<string, object="">> getPersonListMap1(
31             String jsonstring) {
32         List<map<string, object="">> list = new ArrayList<map<string, object="">>();
33         try {
34             list = JSON.parseObject(jsonstring,
35                     new TypeReference<list<map<string, object="">>>() {
36                     }.getType());
37  
38         } catch (Exception e) {
39             // TODO: handle exception
40         }
41  
42         return list;
43     }
44 }</list<map<string,></map<string,></map<string,></map<string,></t></t></t></t></t></t></t></t>

 

JSON对于移动设备来说,尤其对于网络环境较差和流量限制的情况下,相对于XML格式的数据传输会更节省流量,传输效率更高。在这三种解析方式中FastJson是效率最高的,推荐使用。

JSON的三种解析方式

标签:

原文地址:http://www.cnblogs.com/McCa/p/4440433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!