2

I have JSON like this

{

    "data": 

[

{

    "id": 1,
    "Name": "Choc Cake",
    "Image": "1.jpg",
    "Category": "Meal",
    "Method": "",
    "Ingredients": 

[

{

    "name": "1 Cup Ice"

},

        {
            "name": "1 Bag Beans"
        }
    ]

},
{

    "id": 2,
    "Name": "Ice Cake",
    "Image": "dfdsfdsfsdfdfdsf.jpg",
    "Category": "Meal",
    "Method": "",
    "Ingredients": 

[

                {
                    "name": "1 Cup Ice"
                }
            ]
        }
    ]

}

I am using JSON Object to de-Serialize the data

this is what i am trying to

JSONObject jsonObj = new JSONObject(jsonStr);
String first = jsonObj.getJSONObject("data").getString("name");
System.out.println(first);

But a Cant seem to get the name or anything Not sure what i am doing wrong?

and then i am trying to display it into a listview but haven't got to that part yet

Tash Nar
  • 471
  • 2
  • 5
  • 10

4 Answers4

3

data is a JSON Array, not a JSONObject try: jsonObj.getJSONArray("data").getJSONObject(0).getString("name") also note the difference between getString and optString, if you don't want an exception on null use the later.

  • Ok This works, Now how would i loop it to display in listview this is what i have right now: `JSONObject jsonObj = new JSONObject(jsonStr); int length = jsonObj .length(); for(int i=0; i – Tash Nar Sep 10 '15 at 05:45
2

First parse your Json from below method,

private ArrayList<String> getStringFromJson(String jsonStr)
{
    ArrayList<String> mNames = new ArrayList<String>();
    JSONArray array = new JSONArray(jsonStr);
    for (int i = 0; i < array.length(); i++) {
        JSONObject row = array.getJSONObject(i);
        mNames= row.getString("Name");
    }
    return mNames;
}
Arsal Imam
  • 2,882
  • 2
  • 24
  • 35
  • This method will get all the names from your Json...! Your JSON is a complex formatted JSON, you need to understand some major features of it...! – Arsal Imam Sep 10 '15 at 03:04
2
try {
     JSONObject jsonObj = new JSONObject(jsonStr);
     jsonObj.getJSONArray("data").getJSONObject(0).getString("name")
} catch (JSONException e) {

}

Data is a json array. Use getJsonObject for json objects.

Refer to this example to create a ListView and populate it's adapter with data from a json object.

Community
  • 1
  • 1
0

Use GSON instead JSON. Hope it helps you.

  GsonBuilder gsonBuilder = new GsonBuilder();
  Gson gson = gsonBuilder.create();
    List<Data> datas= new ArrayList<Data>();
    datas= Arrays.asList(gson.fromJson(jsonString, Data[].class));

    public class Ingredients {
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        private String name;
    }




public class Data {

    private int id;
    private String Name;
    private String Image;
    private String Category;
    private String Method;

    public List<Ingredients> getIngredients() {
        return Ingredients;
    }

    public void setIngredients(List<Ingredients> ingredients) {
        Ingredients = ingredients;
    }

    private List<Ingredients> Ingredients = new ArrayList<Ingredients>();


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getImage() {
        return Image;
    }

    public void setImage(String image) {
        Image = image;
    }

    public String getCategory() {
        return Category;
    }

    public void setCategory(String category) {
        Category = category;
    }

    public String getMethod() {
        return Method;
    }

    public void setMethod(String method) {
        Method = method;
    }



}
Joy Helina
  • 378
  • 1
  • 5
  • 17