Aydomir
Aydomir

Reputation: 21

How Parse JSON in Java

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!

Upvotes: 1

Views: 251

Answers (1)

Nambi
Nambi

Reputation: 12042

your jsonObject is within the jsonArray

Do like this

JSONArray jarray= new JSONArray(EntityUtils.toString(responseEntity));
String error=jarray.getJSONObject(0).getString("error");

Upvotes: 2

Related Questions