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);
            }
        }
maxcc
  • 25
  • 1
  • 1
  • 3

6 Answers6

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

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
  • Thanks. I know yeah since I would have done obj[0].status in javascript but it didn't work here. – maxcc Feb 22 '17 at 05:10
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"));
Maaz Patel
  • 756
  • 2
  • 7
  • 20
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"));
Zaki Pathan
  • 1,762
  • 1
  • 13
  • 26
  • it says can't resolve toString() which library do I import? – maxcc Feb 22 '17 at 04:57
  • 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")); – Zaki Pathan Feb 22 '17 at 04:58
  • if this works mark this as a correct answer so others getting help from this. Happy to help you :) :) :) – Zaki Pathan Feb 22 '17 at 05:01
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);
        }
    }
sasikumar
  • 12,540
  • 3
  • 28
  • 48
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

Muhammad Usman
  • 863
  • 1
  • 11
  • 18
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);
            }
        }
Zakir hussain
  • 392
  • 1
  • 10