-3

I have a json response as shown below

[
    {
        "id": "1",
        "name": "b day",
        "date": "2015-12-08",
        "start_time": "00:50:02",
        "end_time": "05:00:00"
    },
    {
        "id": "2",
        "name": "game",
        "date": "2015-11-18",
        "start_time": "00:00:02",
        "end_time": "09:10:00"
    }
]

My android code to retrieve json is given below

int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { 
    //success
    BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user
  • 87
  • 8

3 Answers3

0

You can get JSONArray like below.

JSONArray jsonArray = new JSONArray(responseString);

From array you can get JSONObject like below.

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

        // This will get first JSONObject from JSONArray.
        JSONObject jObject = jsonArray.getJSONObject(i);

        // Get all key from array using JSONObject.

        String id = jObject.getString("id");
        String name = jObject.getString("name");
        String date = jObject.getString("date");
        String startTime = jObject.getString("start_time");
        String endTime = jObject.getString("end_time");

}
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
0

Try this:

try
{
    JSONObject issueObj = new JSONObject(JSONString);


    Iterator iterator = issueObj.keys();

    while (iterator.hasNext())
    {
        String key = (String) iterator.next();
        JSONObject issue = issueObj.getJSONObject(key);

        int pubKey = Integer.valueOf(issue.optString("id"));

        mylist.add(pubKey);

        JSONObject json = new JSONObject(JSONString);
        JSONObject jArray = json.getJSONObject(String.valueOf(pubKey));

        nameString = jArray.getString("name");
        dateString = jArray.getString("date");
        start_timeString = jArray.getString("start_time");
        end_timeString = jArray.getString("end_time");
    }
} catch (JSONException e)
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Aspicas
  • 4,498
  • 4
  • 30
  • 53
0
Use this code

     int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { //success
    BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
}
        json = response.toString();
    JSONArray jsonArray = new JSONArray(json);
    ArrayList<String> al_ids=new ArrayList<String>();
    Arraylist<String> al_name=new ArrayList<String>();
    Arraylist<String> al_date=new ArrayList<String>();
    Arraylist<String> al_start_time=new ArrayList<String>();
    Arraylist<String> al_end_time=new ArrayList<String>();

    for(int i = 0 ; i< jsonArray.length() ; i++){
        // This will get first JSONObject from JSONArray.
        JSONObject jObject = jsonArray.getJSONObject(i);

        // Get all key from array using JSONObject.

        String id = jObject.getString("id");
        String name = jObject.getString("name");
        String date = jObject.getString("date");
        String startTime = jObject.getString("start_time");
        String endTime = jObject.getString("end_time");
        al_ids.add(id);
        al_name.add(name);
        al_date.add(date);
        al_start_time.add(startTime);
        al_end_time.add(endTime);
}
    //here you can set text to Your TextView by getting position of arraylist