Parsing json array with no name in android

13,372

Solution 1

Try this..

JSONArray jsonarray = new JSONArray(json);

for (int i = 0; i < jsonarray.length(); i++) {

       JSONObject jsonobj = jsonarray.getJSONObject(i);

      System.out.println("categoryId : " + i + " = " + jsonobj.getString("categoryId"));
       System.out.println("Title : " + i + " = " + jsonobj.getString("Title"));
       System.out.println("songs : " + i + " = " + jsonobj.getString("songs"));
}

Solution 2

Try this,

JSONArray array1=new JSONArray(json);
for(int i=0;i<array1.length;i++)
{
JSONObject obj1 = array1.getJSONObject(i);
    String categoryId = obj1.getString("categoryId");
    String Title = obj1.getString("Title");
    String songs = obj1.getString("songs");
}

Solution 3

You can use:

JSONArray a = new JSONArray(myJsonString);

Solution 4

Try this:

JSONArray arr = new JSONArray(jsonString);

for (int i = 0; i < arr.length(); i++) {
    JSONObject obj = arr.getJSONObject(i);

     String title = obj.getString("Title");
}
Share:
13,372
Maha Mahmoud
Author by

Maha Mahmoud

Updated on June 14, 2022

Comments

  • Maha Mahmoud
    Maha Mahmoud almost 2 years

    I have a Json Array as string with no name and I want to parse it how can i do it in android ?

    My JsonArray is :

      [
      { 
        "categoryId":1,
        "Title":"Rock",
        "songs":null
      },
        { 
        "categoryId":2,
        "Title":"Jaz",
        "songs":null
        }
      ]