Android解析JSON响应

首先贴上JSON响应数据,我们要解析的就是这段数据:

 1 [
 2   {
 3     "phone": [
 4       "12345678",
 5       "87654321"
 6     ],
 7     "name": "路飞",
 8     "age": 17,
 9     "address": {
10       "country": "japan",
11       "province": "sakura"
12     },
13     "married": false
14   },
15   {
16     "phone": [
17       "123345378",
18       "8354321"
19     ],
20     "name": "樱木",
21     "age": 18,
22     "address": {
23       "country": "china",
24       "province": "jiangsu"
25     },
26     "married": false
27   },
28   {
29     "phone": [
30       "22225678",
31       "44444444"
32     ],
33     "name": "悟空",
34     "age": 100,
35     "address": {
36       "country": "china",
37       "province": "那美克星"
38     },
39     "married": false
40   }
41 ]

然后贴源代码:

 
 3 import java.io.BufferedReader;
 4 import java.io.InputStreamReader;
 5 import java.util.ArrayList;
 6 import java.util.List;
 7 
 8 import org.apache.http.HttpEntity;
 9 import org.apache.http.HttpResponse;
10 import org.apache.http.client.HttpClient;
11 import org.apache.http.client.methods.HttpGet;
12 import org.apache.http.impl.client.DefaultHttpClient;
13 import org.apache.http.params.HttpConnectionParams;
14 import org.apache.http.params.HttpParams;
15 import org.json.JSONArray;
16 import org.json.JSONObject;
17 
18 import android.app.Activity;
19 import android.os.Bundle;
20 import android.widget.TextView;
21 
22 public class MainActivity extends Activity {
23 
24     private TextView textView;
25     private List<Person> persons;
26     @Override
27     public void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.main);
30         try {
31             StringBuffer sb = new StringBuffer();
32             String url = "http://*********/test/testFunc";//请求数据的url
33             String body = getContent(url);
34             JSONArray array = new JSONArray(body);
35             persons = new ArrayList<Person>();
36             String result = null;
37             for(int i=0;i<array.length();i++){
38                 JSONObject obj = array.getJSONObject(i);
39                 Person p = new Person();                
40                 
41                 p.setName(obj.get("name").toString());
42                 
43                 p.setMarried(obj.getBoolean("married"));
44                 
45                 p.setAge(obj.getInt("age"));
46                 //read phoneNumber
47                 JSONArray phoneArray = obj.getJSONArray("phone");
48                 String[] phoneStr = new String[phoneArray.length()];
49                 for(int j=0;j<phoneArray.length();j++){
50                     phoneStr[j] = phoneArray.getString(j);
51                 }                
52                 p.setPhone(phoneStr);
53                 //read AddressObject
54                 Address addressTemp = new Address();
55                 JSONObject addressObj = obj.getJSONObject("address");
56                 addressTemp.setCountry(addressObj.getString("country"));
57                 addressTemp.setProvince(addressObj.getString("province"));
58                 p.setAddress(addressTemp);
59                 
60                 persons.add(p);
61                 
62             }
63             textView = (TextView)findViewById(R.id.textview);
64             for(int i=0;i<array.length();i++){
65                 result += persons.get(i).toString();
66             }
67             textView.setText(result);
68             
69         } catch (Exception e) {
70         }
71     }
72     
73     
74     private String getContent(String url) throws Exception{
75         StringBuilder sb = new StringBuilder();
76         HttpClient client = new DefaultHttpClient();
77         HttpParams httpParams = client.getParams();
78         HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
79         HttpConnectionParams.setSoTimeout(httpParams, 5000);
80         HttpResponse response = client.execute(new HttpGet(url));
81         HttpEntity entity = response.getEntity();
82         if( null != entity ){
83             BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
84             String line = null;
85             while(null != (line = reader.readLine())){
86                 sb.append(line+"\n");
87             }
88             reader.close();
89         }
90         return sb.toString();
91     }
92 }

记得加网络权限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

然后贴实体类:

 1 public class Person {
 2     private String name;
 3     private Integer age;
 4     private Boolean married;
 5     private String[] phone;
 6     private Address address;
 7     public Person() {
 8         super();
 9     }
10     public Person(String name, Integer age, Boolean married, String[] phone,
11             Address address) {
12         super();
13         this.name = name;
14         this.age = age;
15         this.married = married;
16         this.phone = phone;
17         this.address = address;
18     }
19     public String getName() {
20         return name;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }
25     public Integer getAge() {
26         return age;
27     }
28     public void setAge(Integer age) {
29         this.age = age;
30     }
31     public Boolean getMarried() {
32         return married;
33     }
34     public void setMarried(Boolean married) {
35         this.married = married;
36     }
37     public String[] getPhone() {
38         return phone;
39     }
40     public void setPhone(String[] phone) {
41         this.phone = phone;
42     }
43     public Address getAddress() {
44         return address;
45     }
46     public void setAddress(Address address) {
47         this.address = address;
48     }
49     @Override
50     public String toString() {
51         String phoneStr = "";
52         for(int i=0;i<phone.length;i++){
53             phoneStr += phone[i]+".";
54         }
55         return "name:"+name+";age:"+age+";married:"+married+";phone:"+phoneStr+";address:'country="+address.getCountry()+",province="+address.getProvince()+"';\n";
56     }
57     
58 }
 1 public class Address {
 2     private String country;
 3     private String province;
 4     public Address(String country, String province) {
 5         super();
 6         this.country = country;
 7         this.province = province;
 8     }
 9     public Address() {
10         super();
11     }
12     public String getCountry() {
13         return country;
14     }
15     public void setCountry(String country) {
16         this.country = country;
17     }
18     public String getProvince() {
19         return province;
20     }
21     public void setProvince(String province) {
22         this.province = province;
23     }
24     
25 }

欢迎拍砖!XD

posted @ 2012-11-20 17:33  黄泉hj  阅读(1134)  评论(0编辑  收藏  举报
沪江在线词典