0

I am new to android and struggling to get the values of the json object. Can someone help me out?

the json returned from the server is {"status":"active"}

I'm using Android Asynchronous Http Client library..

public void onSuccess(int statusCode, Header[] headers, JSONObject response) { try { JSONArray j = new JSONArray(response); t.setText(j.getJSONObject(0).getString('status'));//this doesn't set the text to the status } catch (JSONException e) { Log.e("MYAPP", "unexpected JSON exception", e); } } 
4

6 Answers 6

1

It's already a JSON Object as incoming parameter You don't need to create external object of JSON Array or JSON Object Try This.

t.setText(response.getString("status")); 
Sign up to request clarification or add additional context in comments.

Comments

1

you can use

JSONObject jsonObject = new JSONObject(response); 

because {"status":"active"} is a JSONObject.

and use

 t.setText(jsonObject.getString("status")); 

Full code is

public void onSuccess(int statusCode, Header[] headers, JSONObject response) { try { JSONObject jsonObject = new JSONObject(response); t.setText(jsonObject.getString("status")); } catch (JSONException e) { Log.e("MYAPP", "unexpected JSON exception", e); } } 

As per other answers It is waste to create a new JSONObject. So use it directly

 public void onSuccess(int statusCode, Header[] headers, JSONObject response) { try { t.setText(response.getString("status")); } catch (JSONException e) { Log.e("MYAPP", "unexpected JSON exception", e); } } 

Note: creating more instances is for bad performance

1 Comment

Thanks. I know yeah since I would have done obj[0].status in javascript but it didn't work here.
0

replace this:

JSONArray j = new JSONArray(response); t.setText(j.getJSONObject(0).getString("status")); 

with:

JSONObject j = new JSONObject(response); t.setText(j.getString("status")); 

if you getting String in response you have to try this hope it helps you

EDIT:

But in your case JSONObject is coming in response So you can try directly like this:

t.setText(response.getString("status")); 

3 Comments

it says can't resolve toString() which library do I import?
check my edited answer. try t.setText(response.getString("status")); directly in place of JSONArray j = new JSONArray(response); t.setText(j.getJSONObject(0).getString("status"));
if this works mark this as a correct answer so others getting help from this. Happy to help you :) :) :)
0

Use direct JSONObject for getting value

 public void onSuccess(int statusCode, Header[] headers, JSONObject response) { try { t.setText(response.getString("status")); } catch (JSONException e) { Log.e("MYAPP", "unexpected JSON exception", e); } } 

Comments

0

It seems you are not familiar with JSON object and JSON array. I will recommend you to print your response on Log.i("response", response.toString()) then after checking response from JSON validator You perform actual stuff.

see JSON Array and JSON object also

Comments

0

Use optString instead of getString,because if you are using getString and the key "status" not exit then app may be crash.

public void onSuccess(int statusCode, Header[] headers, JSONObject response) { try { t.setText(j.getJSONObject(0).optString('status')); } catch (JSONException e) { Log.e("MYAPP", "unexpected JSON exception", e); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.