json对象无法在android中转换为jsonarray

时间:2014-01-16 08:30:17

标签: java android json

       01-16 08:16:19.210: W/System.err(1394): org.json.JSONException: Value 

    {"GetDoctorsListResult":[
    {"Name":"Sujay Sharma","DoctorID":154,"Clinics":null,"speciality":"Allergy and immunology","Locality":"Mumbai","Address":"Abcc Building  "}
        ,{"Name":"Samir Tapiawala","DoctorID":159,"Clinics":null,"speciality":"Homeopath","Locality":"Mumbai","Address":"57\/101, Jawahar Nagar RD. NO. 6, GOREGAON WEST "}
        ,{"Name":"Sahil Kuma","DoctorID":171,"Clinics":null,"speciality":"Aerospace Medicine","Locality":"Mumbai","Address":"Digvija  "}
        ,{"Name":"Himesh Jagtap","DoctorID":180,"Clinics":null,"speciality":"Bariatric Surgery","Locality":"Mumbai","Address":"Abc- Society  "}
        ,{"Name":"Aarnav Prabhudesai","DoctorID":190,"Clinics":null,"speciality":"Dentistry","Locality":"Mumbai","Address":"Joyvilla Jawahar Nagar  "}
        ,{"Name":"Neeharika Saxana","DoctorID":197,"Clinics":null,"speciality":"Gynaecologist","Locality":"Mumbai","Address":"Joyvilla, Jawahar Nagar  "}
        ,{"Name":"Neeharika Saxena","DoctorID":205,"Clinics":null,"speciality":"Gynaecologist","Locality":"Mumbai","Address":"Joyvilla  "}
        ,{"Name":"Ravi Sharma","DoctorID":207,"Clinics":null,"speciality":"Ayurvedacharya","Locality":"Mumbai","Address":"Q\/02  "}
        ,{"Name":"Prashant Bhatt","DoctorID":209,"Clinics":null,"speciality":"Dentistry","Locality":"Mumbai","Address":"202 Anand Vihar , Bldg No-2 , D-wing , Bhawani Chowk , b--cabin Road , Ambernath(e). Bhawani Chowk"}
        ,{"Name":"Samidha Sen","DoctorID":210,"Clinics":null,"speciality":"Addiction Medicine","Locality":"Mumbai","Address":"S\/09  "}
        ,{"Name":"Subodh Mehta","DoctorID":212,"Clinics":null,"speciality":"Orthopaedic Surgery","Locality":"Mumbai","Address":"Opera  "}]} of type org.json.JSONObject cannot be converted to JSONArray

这是我的活动代码:

  class DownloadTask extends AsyncTask<String, Void, Object> {
    protected Boolean doInBackground(String... params) {
        try {
            Thread.sleep(4000); // Do your real work here
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return true; // Return your real result here
    }

    protected void onPostExecute(Object result) {
        // Pass the result data back to the main activity
        DoctorSearchActivity.this.data = result;
        if (DoctorSearchActivity.this.progressdialog != null) {
            DoctorSearchActivity.this.progressdialog.dismiss();
        }
        try {
            JSONStringer geneology = new JSONStringer()


                    .object().key("Params").object().key("TypesOnSearch")
                    .value("name").key("CharactersToSearch")
                    .value("s")
                    .endObject();

            JSONArray results = new JSONArray();
            results = BC
                    .returnJSONArray(geneology,
                            "http://192.168.2.27/HYEHR_WCFService/DoctorService.svc/GetDoctorsList");
            if (results.length() == 0) {
                Toast.makeText(
                        DoctorSearchActivity.this,
                        "Error Occured while loading data.Try again later.",
                        Toast.LENGTH_LONG).show();
            }
            // for (int i = 0; i < results.length(); i++) {
            // Log.v("data", results.getString(i));
            // }
            // aTable = BC.appendRows(aTable, results,
            // DoctorSearchActivity.this);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

}

和我的json功能 : public JSONArray returnJSONArray(JSONStringer JsonString,String url){

    results = new JSONArray();

    try {
        HttpPost request = new HttpPost(url);
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");

        // Build JSON string
        StringEntity entity = new StringEntity(JsonString.toString());
        request.setEntity(entity);
        Log.v("data", request + JsonString.toString());
        // Log.v("data sender",request.setEntity(entity));
        // Send request to WCF service
        DefaultHttpClient httpClient1 = new DefaultHttpClient();
        HttpResponse response = httpClient1.execute(request);
        Log.v("response code", response.getStatusLine().getStatusCode()
                + "");
        HttpEntity responseEntity = response.getEntity();
        // Read response data into buffer
        char[] buffer = new char[(int) responseEntity.getContentLength()];
        InputStream stream = responseEntity.getContent();
        InputStreamReader reader = new InputStreamReader(stream);
        reader.read(buffer);
        stream.close();
        results = new JSONArray(new String(buffer));
        Log.v("results length : ", results.length() + "");
    }
         catch (Exception e) {
        // i mean sending data without key
        // TODO: handle exception
        e.printStackTrace();
    }
    return results;
}

我收到此错误我在jsonobject中传递数据并在jsonarray中接收数据。

4 个答案:

答案 0 :(得分:3)

尝试替换它:

results = new JSONArray(new String(buffer));

由此:

results = new JSONObject(new String(buffer)).getJSONArray("GetDoctorsListResult");

答案 1 :(得分:2)

来自URL的JSON的根元素是JSONObject,您应首先将其解析为JSONObject然后 从对象获取GetDoctorsListResult这是一个数组。

答案 2 :(得分:2)

HttpEntity entity = httpResponse.getEntity(); 
String result =  EntityUtils.toString(entity);
JSONObject jsonObject=new JSONObject(result);
JSONArray jsonArray=jsonObject.getJSONArray("GetDoctorsListResult");

获得回复后使用此代码。

答案 3 :(得分:1)

我认为对象的第一个元素GetDoctorsListResult是JSON数组。所以你必须在json数组中取其值。

REFER THIS THREADTHIS ONE

JSONArray DoctorsList= jsonResponse.getJSONArray("GetDoctorsListResult");