【转】几种不同格式的json解析

JSON的基本数据格式有这几种:

1.一个JSON对象——JSONObject

{"name":"胡小威" , "age":20 , "male":true}

2.一个JSON数组——JSONArray

[{"name":"胡小威" , "age":20 , "male":true},{"name":"赵小亮" , "age":22 , "male":false}]

3.复杂一点的JSONObject

{"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}}

4.复杂一点的JSONArray

[

{"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}},

{"name":"赵小亮", "age"=22, "male":false, "address":{"street":"九州港", "city":"珠海","country":"中国"}}

]

5.来个更复杂的,这是我从爱帮公交上扒取的数据

 {"buses":{"bus":[{"last_foot_dist":"0","time":"37","segments":{"segment":[{"line_name":"立珊专线(中南大学学生公寓-长沙火车站)","foot_dist":"362","stat_xys":"","stats":"岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);牛耳教育(南阳街口);韭菜园;曙光路口;长岛路口;长沙火车站","end_stat":"长沙火车站","line_xys":"","line_dist":"7535","start_stat":"岳麓山南"}]},"foot_dist":"362","dist":"7897"},{"last_foot_dist":"0","time":"41","segments":{"segment":[{"line_name":"旅1路(科教新村-长沙火车站)","foot_dist":"337","stat_xys":"","stats":"岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);蔡锷中路口;韭菜园;乔庄;曙光路口;长岛路口;五一东村;车站路口;长沙火车站","end_stat":"长沙火车站","line_xys":"","line_dist":"7822","start_stat":"岳麓山南"}]},"foot_dist":"337","dist":"8159"}]}}

 

接下来是解析部分

一、直接解析

1.{"name":"胡小威" , "age":20 , "male":true}

 

Java代码  收藏代码
  1. public void parseJsonObject(String json) {  
  2.         try {  
  3.             JSONObject jsonObject = new JSONObject(json);  
  4.             String name = jsonObject.getString("name");  
  5.             int age = jsonObject.getInt("age");  
  6.             boolean male = jsonObject.getBoolean("male");  
  7.             //接下来该干嘛干嘛  
  8.         } catch (JSONException e) {  
  9.             // TODO Auto-generated catch block  
  10.             e.printStackTrace();  
  11.         }  
  12.     }  

 

 

2.[{"name":"胡小威" , "age":20 , "male":true},{"name":"赵小亮" , "age":22 , "male":false}]

 

 

Java代码  收藏代码
  1. public void parseJSONArray(String json) {  
  2.         try {  
  3.             JSONArray jsonArray = new JSONArray(json);  
  4.             for (int i = 0; i < jsonArray.length(); i++) {  
  5.                 jsonObject = jsonArray.getJSONObject(i);  
  6.                 String name = jsonObject.getString("name");  
  7.                 int age = jsonObject.getInt("age");  
  8.                 boolean male = jsonObject.getBoolean("male");  
  9.                 //接下来该干嘛干嘛  
  10.             }  
  11.         } catch (JSONException e) {  
  12.             // TODO Auto-generated catch block  
  13.             e.printStackTrace();  
  14.         }  
  15.     }  

  

 

3.{"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}}

 

 

Java代码  收藏代码
  1. public void parseJsonPerson(String json) {  
  2.         try {  
  3.             JSONObject jsonObject = new JSONObject(json);  
  4.             String name = jsonObject.getString("name");  
  5.             int age = jsonObject.getInt("age");  
  6.             boolean male = jsonObject.getBoolean("male");  
  7.             JSONObject addressJSON = jsonObject.getJSONObject("address");  
  8.             String street = addressJSON.getString("street");  
  9.             String city = addressJSON.getString("city");  
  10.             String country = addressJSON.getString("country");  
  11.             Address address = new Address(street, city, country);  
  12.             Person person = new Person(name, age, male, address);  
  13.         } catch (JSONException e) {  
  14.             // TODO Auto-generated catch block  
  15.             e.printStackTrace();  
  16.         }  
  17.     }  

 

4.

[

{"name":"胡小威", "age"=20, "male":true, "address":{"street":"岳麓山南", "city":"长沙","country":"中国"}},

{"name":"赵小亮", "age"=22, "male":false, "address":{"street":"九州港", "city":"珠海","country":"中国"}}

]

 

 

Java代码  收藏代码
  1. public void parseJsonArrayPerson(String json) {  
  2.         List<Person> persons = new ArrayList<Person>();  
  3.         try {  
  4.             jsonArray = new JSONArray(json);  
  5.             for (int i = 0; i < jsonArray.length(); i++) {  
  6.                 jsonObject = jsonArray.getJSONObject(i);  
  7.                 String name = jsonObject.getString("name");  
  8.                 int age = jsonObject.getInt("age");  
  9.                 boolean male = jsonObject.getBoolean("male");  
  10.                 JSONObject addressJSON = jsonObject.getJSONObject("address");  
  11.                 String street = addressJSON.getString("street");  
  12.                 String city = addressJSON.getString("city");  
  13.                 String country = addressJSON.getString("country");  
  14.                 Address address = new Address(street, city, country);  
  15.                 Person person = new Person(name, age, male, address);  
  16.                 persons.add(person);  
  17.                 Log.v("juno", person.toString());  
  18.             }  
  19.         } catch (JSONException e) {  
  20.             // TODO Auto-generated catch block  
  21.             e.printStackTrace();  
  22.         }  
  23.         Log.v("juno", persons.toString());  
  24.     }  

 

5. {"buses":{"bus":[{"last_foot_dist":"0","time":"37","segments":{"segment":[{"line_name":"立珊专线(中南大学学生公寓-长沙火车站)","foot_dist":"362","stat_xys":"","stats":"岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);牛耳教育(南阳街口);韭菜园;曙光路口;长岛路口;长沙火车站","end_stat":"长沙火车站","line_xys":"","line_dist":"7535","start_stat":"岳麓山南"}]},"foot_dist":"362","dist":"7897"},{"last_foot_dist":"0","time":"41","segments":{"segment":[{"line_name":"旅1路(科教新村-长沙火车站)","foot_dist":"337","stat_xys":"","stats":"岳麓山南;湖南师大;二里半;岳麓山北;市四医院;华图教育(太平街口);蔡锷中路口;韭菜园;乔庄;曙光路口;长岛路口;五一东村;车站路口;长沙火车站","end_stat":"长沙火车站","line_xys":"","line_dist":"7822","start_stat":"岳麓山南"}]},"foot_dist":"337","dist":"8159"}]}}

 

Java代码  收藏代码
  1. public List<Salution> parseJSON(String str){  
  2.         Log.d("返回的JSON数据:", str);  
  3.         List<Salution> busList = new ArrayList<Salution>();  
  4.       
  5.         try {  
  6.             JSONObject jsonObject = new JSONObject(str);  
  7.             JSONObject busesJSON = jsonObject.getJSONObject("buses");  
  8.             JSONArray busJSArray=busesJSON.getJSONArray("bus");  
  9.             for(int i=0;i<busJSArray.length();i++){  
  10.                 JSONObject busJSObject = busJSArray.getJSONObject(i);  
  11.                 JSONObject segsJSObject=busJSObject.getJSONObject("segments");  
  12.                 JSONArray segJSArray=segsJSObject.getJSONArray("segment");                
  13.                     Salution st=new Salution();  
  14.                                       
  15.                     for(int j=0;j<segJSArray.length();j++){  
  16.                         if(j==0){  
  17.                             JSONObject segJSObject = segJSArray.getJSONObject(j);  
  18.                             st.setLine_name1(segJSObject.getString("line_name"));  
  19.                             Log.d("line_name", segJSObject.getString("line_name"));  
  20.                             st.setStats1(segJSObject.getString("stats"));  
  21.                             st.setEnd_stat1(segJSObject.getString("end_stat"));  
  22.                             st.setStart_stat1(segJSObject.getString("start_stat"));  
  23.                         }  
  24.                         if(j==1){  
  25.                             JSONObject segJSObject = segJSArray.getJSONObject(j);  
  26.                             st.setLine_name2(segJSObject.getString("line_name"));  
  27.                             Log.d("line_name", segJSObject.getString("line_name"));  
  28.                             st.setStats2(segJSObject.getString("stats"));  
  29.                             st.setEnd_stat2(segJSObject.getString("end_stat"));  
  30.                             st.setStart_stat2(segJSObject.getString("start_stat"));  
  31.                         }  
  32.                     }  
  33.                       
  34.                     busList.add(st);      
  35.             }  
  36.             return busList;  
  37.         } catch (JSONException e) {  
  38.             // TODO Auto-generated catch block  
  39.             e.printStackTrace();  
  40.         }  
  41.         return null;  
  42.     }  

 

 这里我只是简单获取了几个我想要的数据。

 

二、使用JSON类库Gson 来解析JSON

Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。

 

首先,从 code.google.com/p/google-gson/downloads/list下载GsonAPI

Java代码  收藏代码
  1. google-gson-1.7.1-release.zip   

 

 再把gson-1.7.jar copy到libs

可以使用两种方法解析

 

①、通过获取JsonReader对象解析JSON数据:

Java代码  收藏代码
  1. String jsonData = "[{\"username\":\"arthinking\",\"userId\":001},{\"username\":\"Jason\",\"userId\":002}]";  
  2. try{  
  3.     JsonReader reader = new JsonReader(new StringReader(jsonData));  
  4.     reader.beginArray();  
  5.     while(reader.hasNext()){  
  6.         reader.beginObject();  
  7.         while(reader.hasNext()){  
  8.             String tagName = reader.nextName();  
  9.             if(tagName.equals("username")){  
  10.                 System.out.println(reader.nextString());  
  11.             }  
  12.             else if(tagName.equals("userId")){  
  13.                 System.out.println(reader.nextString());  
  14.             }  
  15.         }  
  16.         reader.endObject();  
  17.     }  
  18.     reader.endArray();  
  19. }  
  20. catch(Exception e){  
  21.     e.printStackTrace();  
  22. }  

 

 

 

②、通过把JSON数据映射成一个对象,使用Gson对象的fromJson()方法获取一个对象数组进行操作

Java代码  收藏代码
  1. Type listType = new TypeToken<LinkedList<User>>(){}.getType();  
  2. Gson gson = new Gson();  
  3. LinkedList<User> users = gson.fromJson(jsonData, listType);  
  4. for (Iterator iterator = users.iterator(); iterator.hasNext();) {  
  5.     User user = (User) iterator.next();  
  6.     System.out.println(user.getUsername());  
  7.     System.out.println(user.getUserId());  
  8. }  
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页