0

我在获取一维时遇到问题, JSON请指导我问题出JSON在我的代码中还是我的代码中?

JSON:

{
    "data": {
        "id": "S000010",
        "name": "ZS Solutions",
        "email": "zswebs@gmail.com",
        "phone": "051-1234567",
        "address": "p.o.box 123",
        "about": "im the company\r\nHAhahhaa"
    }
}

Android活动JSON检索代码:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// TODO Auto-generated method stub
try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("abc.php?Id="+id+"");

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse hresponse = httpclient.execute(httppost);
    HttpEntity entity = hresponse.getEntity();
    is = entity.getContent();
    String result=co(is);
    JSONObject json=new JSONObject(result);

    JSONArray a=  json.getJSONArray(data);
    for (int i = 0; i <= a.length(); i++) {

        json = a.getJSONObject(i);
        String  cname=json.getString("name");
        String  cemail=json.getString("email");
        String  cphone=json.getString("phone");
        String  caddress=json.getString("address");
        String  cabout=json.getString("about");
        Log.w("DATA  ","NAME  "+cname+"E-mail  "+cemail+"Phone "+cphone+"ADDRESS"+caddress+"ABOUT"+cabout);
    }


}
catch(Exception e){}
4

1 回答 1

5

JSONArray a= json.getJSONArray(data); < - 这导致异常为

data 不是一个 json 数组,而是一个JSONObject

你的代码应该是

       JSONObject json=new JSONObject(result);

       JSONObject jsonobj=json.getJSONObject("data");

        String  cname=jsonobj.getString("name");
        String  cemail=jsonobj.getString("email");
        String  cphone=jsonobj.getString("phone");
        String  caddress=jsonobj.getString("address");
        String  cabout=jsonobj.getString("about");
于 2013-04-19T18:33:05.937 回答