2014-01-16 49 views
0
 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 

這裏jsonarray是我的活動代碼:JSON對象不能轉換在android系統

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功能 : 公共JSONArray returnJSONArray(JSONStringer JsonString,字符串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。

回答

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

使用此代碼得到響應之後。

+0

感謝@Kirti鬃毛 – PankajSharma

2

來自URL的JSON的根元素是一個JSONObject,您應該先將它解析爲JSONObject,然後 得到GetDoctorsListResult,它是來自對象的數組。

3

嘗試更換此:

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

本:

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

我覺得你的第一個元素對象GetDoctorsListResult是JSON數組。所以你必須在json數組中使用它的值。

REFER THIS THREADTHIS ONE

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