几种不同的json格式解析

Posted 小虾米的java梦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了几种不同的json格式解析相关的知识,希望对你有一定的参考价值。

转连接:

http://blog.csdn.net/whx405831799/article/details/42171191

内容很好

 

给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分情况下都是XML格式或者JSON格式。然后JSON相对XML来说解析相对方便一些,所以先说说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格式解析的主要内容,如果未能解决你的问题,请参考以下文章

ajax以及文件上传的几种方式

轻量级数据格式 —— JSON

json是啥

前端页面显示带格式的json数据

固定格式TXT转json文件

url 参数解析成json格式