1

I have a json array which have no array name. How can I parse a JSON array which dont have a name? This is my array

[
{
    "id": "13",
    "email": "[email protected]",
    "first_name": "jcheck",
    "last_name": "check",
    "country": "india",
    "city": "tvm",
    "zip_code": "695581",
    "phone": "4712584632",
    "status": "Success"
}
]

I tried to convert it to JSON object and also tried to get the String straightly from the array. But I got exceptions both time.

1
  • 1
    JSONArray array = new JSONArray(json) and then JSONObject object = jsonArray.getJSONObject(i); Commented Jun 17, 2013 at 9:45

4 Answers 4

7
JSONArray array = new JSONArray(yourString);
JSONObject obj = array.JSONObject(0)

if you have more than one object:

int size = array.length();
for (int i = 0; i < size; i++) {
   JSONObject obj = array.JSONObject(i);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am not getting JSONObject as JSONOBject and you got to many upvotes also.......... and another haven't single one. Is this fixed IPL? :D
@ Pankaj Kumar. I tried to set your answer, but you deleted it. :(, thanks to blackbelt also.
2

Why not use

    JSONArray array = new JSONArray(jsonString);
    for (int i = 0; i < array.length(); i++)
    {
        JSONObject obj= array.getJSONObject(i);

        String id = obj.getString("id");
        String email = obj.getString("email");
}

Comments

0
String json = //your json array as string;
try {
JSONArray myArray = new JSONArray(json);
catch(JASONException e){
     Log.e("JSON", e.toString());
}

Comments

0

String finaljson=buffer.toString();

            JSONArray array = new JSONArray(finaljson);

            int size = array.length();
            for (int i = 0; i < size; i++)
            {
                JSONObject j= array.getJSONObject(i);

                String Image =j.getString("Image");
                String ImageName=j.getString("Iname");
                String Rating=j.getString("Rating");
                String Description=j.getString("Description");
                return Image +"/"+ ImageName +"/"+ Rating +"/"+ Description;
            }

1 Comment

Can you provide an explanation of why this example is preferable compared to the existing accepted answer?

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.