gpt4 book ai didi

java - android中json对象无法转换为jsonarray

转载 作者:行者123 更新时间:2023-12-01 07:07:20 25 4
gpt4 key购买 nike

       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

这是我的 Activity 代码:

  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 中接收数据时收到此错误。

最佳答案

尝试替换这个:

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

通过这个:

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

关于java - android中json对象无法转换为jsonarray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21156756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com