Pregunta

This is my Java code:

...
HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();
JSONObject serverResponse = new JSONObject(EntityUtils.toString(responseEntity));
String error = serverResponse.getString("error");
text1.setText(error);
...

I can not parse JSON.

JSON:

[{"error":"1"}]

Thank you!

¿Fue útil?

Solución

your jsonObject is within the jsonArray

Do like this

JSONArray jarray= new JSONArray(EntityUtils.toString(responseEntity));
String error=jarray.getJSONObject(0).getString("error");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top