2

suppose I have a JSON that prints out

{"_id"              :"4e3f2c6659f25a0f8400000b",
 "confirmation_code":"TWLNX8BT",
 "confirmed"        :true,
 "created_at"       :"2011-08-08T00:23:02+00:00",
 "email_address"    :"dd5dc43ea6bf12ec604b0a7025b94105d419616b",
 "first_name"       :"sean",
 "invites"          :[],
 "last_name"        :"pan",
 "raw_email_address":null,
 **"tracking_users" :[{ 
                        "_id"       :"4e407f0659f25a1ce9000007",
                        "active"    :true,
                        "first_name":"Sean",
                        "last_name" :"Pan",
                        "user_id"   :"4e3da65e59f25a3956000005"
                     },{
                        "_id"       :"4e407f7a59f25a1d19000007",
                        "active"    :true,
                        "first_name":"Sean",
                        "last_name" :"Pan",
                        "user_id"   :"4e3da65e59f25a3956000005"
                     },{
                        "_id"       :"4e4085c959f25a204b000004", 
                        "active"    :true,
                        "first_name":"Sean",
                        "last_name" :"Pan",
                        "user_id"   :"4e3da65e59f25a3956000005"
                     }],
 "updated_at"       :"2011-08-08T06:44:31+00:00",
 "user_id"          :137141}**

in the tracking users part I have three "different" (they're the same for testing purposes) JSON strings within the original JSON. How do I go through the inner parameter (user_id[0]),(user_id[1]),(user_id[2])... of tracking_users in a for loop for android?

I am turning my JSON into a string and then using obj = new org.json.JSONObject(response) to change it into an object then I use String trackingusers=obj.getString("tracking_users") to get the three objects in the tracking_users variable.

Thanks

3 Answers 3

4

Get tracking_users as JSONArray, then loop them as JSONObject, and with the JSONObject, you can get it's properties, try this:

JSONArray tracking_users = obj.getJSONArray("tracking_users");
for (int i = 0; i < tracking_users.length(); i++) {
    JSONObject user = tracking_users.getJSONObject(i);
    String _id = user.getString("_id");
    and etc..
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use getJSONArray("tracking_users") and process each item in the array as an JSONObject.

2 Comments

Thanks that helped but can you tell me how to process each item as a JSONObject? For now I have JSONArray jsonarray=obj.getJSONArray("tracking_users"); jsonarray[0] does nothing etc
It depends on what you actually need to do, but basically extract the data you need using getString(), etc. See the first answer for some sample code.
1

JSON is composed from Objects and Array of objects. The whole result string is an object. So you loaded it fine. After that, you have to process tracking_users as Array of Objects. So use:

JSONAeeay users = obj.getJSONArray("tracking_users");

and with this, you can cycle through the objects:

int users_count = users.length();
for (int i=0; i<users_count; i++)
{
    users.getJSONObject(i)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.