-1

I have a JSON like this:

{
    "Output": [
        {
            "Description": "Desc1",
            "Amount": 40,
            "TotalQuota": 40,
            "QuotaPerOne": 2
        },
        {
            "Description": "Desc2",
            "Amount": 50,
            "TotalQuota": 60,
            "QuotaPerOne": 3
        }
    ]
}

I try to get the value of column "Description" of the above 2 items, and put in a Arraylist by the code below:

try{
        JSONArray output = importData.getJSONArray("Output");

        ArrayList<String>Title = new ArrayList<String>();
        for(int i=0;i<output.length();i++) {
            String bookingInfo = output.getJSONObject(i).toString();
            bookingList.add(bookingInfo);
        }
    }catch (Exception e) {
        System.out.println(e.toString());
    }

But I only get the whole column of item1 and item2. My expect result is to put "Desc1","Desc2" into ArrayList Title, please help!

Paritosh
  • 2,097
  • 3
  • 30
  • 42
kris ho
  • 51
  • 1
  • 9
  • *But I only get the whole column of item1 and item2.* then do not use toString() but the proper method to get proper property ... – Selvin Oct 20 '15 at 10:40
  • Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Selvin Oct 20 '15 at 10:41

2 Answers2

2

To get only Description from the JSON

ArrayList<String> Title = new ArrayList<String>();
for(int i=0;i<output.length();i++) {
  JSONObject jsonObject = output.getJSONObject(i)
  // Check if the jsonObject contains "Description Tag"
  if(jsonObject.has("Description")){
      String descrption = jsonObject.getString("Description");
      Title.add(descrption);
  }
} 
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
0

i think you are using an icorrect method use this instead, this works for me:

for(int i = 0;i<ja.length();i++)
                        {
                            JSONObject jo = ja.getJSONObject(i);
                            User u = new User();
                            u.userid = jo.getString("userId");//add descripton here
                            u.id = jo.getString("id");//amount and so on
                            u.title = jo.getString("title");
                            u.body = jo.getString("body");
                            list_user.add(u);
                            h.sendEmptyMessage(i);
                            Thread.sleep(300);

                        }
}

you just need to change it accorduing to ur json data, like you need to add booklist instead of user and so on

Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62