Error JSON "Valor al resultado del tipo Java.lang.String no se puede convertir a JSONArray" en Android
Pregunta
Yo usé el Enlace para JSON RPC. Estoy recibiendo una respuesta como se esperaba. Pero cuando trato de analizar la respuesta, me está dando un error JSON.
Mi código:
JSONEntity entity = new JSONEntity(jsonRequest);
HttpPost request = new HttpPost("http://192.168.1.150/jsondemo12/service.asmx");
request.setEntity(entity);
HttpResponse response = httpClient.execute(request);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream content = httpEntity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content,"iso-8859-1"),8);
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
content.close();
} else {
Log.e(AndroidJSONActivity.class.toString(), "Failed to download file");
}
strJSONValue=builder.toString();
txtViewParsedValue.append("\n+++++++++++++\n"+strJSONValue+"\n");
try {
parseJSON();
} catch (JSONException e) {
Log.e("error","Error while parsing!!!");
e.printStackTrace();
}
Log.e("response", strJSONValue);
public void parseJSON() throws JSONException
{
String attr1="",attr2="";
jsonObject = new JSONObject(strJSONValue);
JSONArray result = jsonObject.getJSONArray("result"); <- Error in this line!!!
for(int i=0;i < result.length();i++){
JSONObject e = result.getJSONObject(i);
attr1 = "ExhibitorID: "+ e.getString("ExhibitorID");
attr2 = "ExhibitorName: "+e.getString("ExhibitorName");
}
strParsedValue=attr1+"\n"+attr2;
Log.d("Parse", attr1);
Log.d("Parse", attr2);
txtViewParsedValue.append("\n**********\nParsed Value: \n");
txtViewParsedValue.append(strParsedValue);
}
El resultado que obtengo en "strjsonValue" es el formato de cadena, sin las cotizaciones dobles iniciales y terminadas. Me gusta:
{"id":2,"result":"[
{\"ExhibitorID\":42, etc....}
]"}
La cadena de resultados es según el requisito, pero no puedo analizar la cadena en el objeto JSON según el requisito. Da un error en logCat: org.json.JSONException: Value <content of the string> at result of type java.lang.String cannot be converted to JSONArray
Por favor, ayúdame. Gracias
No hay solución correcta
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow