Carbon
Carbon

Reputation: 133

JSON parsing returning a null value

I am trying to parse the response i get from the network database. I am getting only one value i.e. seller id in response. When i try to pass it in JSON Object, somehow the toast after it doesn't execute and the toast after it is not executed.

I know there are many related questions but i can't find whats wrong... please help. Thanks.

        try {

        JSONArray jArray = new JSONArray(stuff.toString());

        Toast.makeText(this, "len: " + jArray.length(), 1000).show(); // length is 1

        for (int i = 0; i < jArray.length(); i++) {
            Toast.makeText(this, "Arr: " + stuff, 1000).show(); 
                              // Arr: [{"seller_id":"5"}]

            JSONObject jObject = jArray.getJSONObject(i);
            j_id = jObject.getString(stuff);

                            // not getting executed
            Toast.makeText(this, "JSON: " + j_id, 1000).show();
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Toast.makeText(this, "View: " + j_id, 1000).show(); // j_id is giving null value

This is my json data [{"seller_id":"5"}] i am trying to get the value of seller id (ie 5 here) out of it.

Upvotes: 1

Views: 541

Answers (2)

Ravi the rko
Ravi the rko

Reputation: 51

After the following code:

j_id = jObject.getString(stuff);

add textview.settext(j_id);

and change this textview with your textview name

and if its not working than show your complete json data.

Upvotes: 0

Jitendra Kumar. Balla
Jitendra Kumar. Balla

Reputation: 1213

There is a problem in jObject.getString() method args

JSONObject jObject = jArray.getJSONObject(i);
        j_id = jObject.getString(stuff);

Your giving the array. It should the name of the object i.e seller_id

so after the modification your code would be j_id = jObject.getString("seller_id");

Upvotes: 2

Related Questions