【问题标题】:json object cannot be converted to jsonarray in androidjson对象无法在android中转换为jsonarray
【发布时间】:2014-01-16 08:30:16
【问题描述】:
       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 中接收数据。

【问题讨论】:

    标签: java android json


    【解决方案1】:

    尝试替换这个:

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

    通过这个:

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

    【讨论】:

      【解决方案2】:
      HttpEntity entity = httpResponse.getEntity(); 
      String result =  EntityUtils.toString(entity);
      JSONObject jsonObject=new JSONObject(result);
      JSONArray jsonArray=jsonObject.getJSONArray("GetDoctorsListResult");
      

      收到响应后使用此代码。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

          REFER THIS THREADTHIS ONE

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

          【讨论】:

            猜你喜欢
            • 2023-04-10
            • 1970-01-01
            • 1970-01-01
            • 2014-10-24
            • 1970-01-01
            • 2016-01-12
            • 2020-07-11
            • 2021-11-20
            • 2017-04-14
            相关资源
            最近更新 更多