-1

Possible Duplicate:
JSON Array iteration in Android/Java

I am developing an app where i am using the following JSONWeb Services Code. Everything is working good and getting response as JSON Array. I am not having any idea how to convert JSON Array to string. The response i am getting is in the following pattern..

Json Respone

[
{
     "Id":101,
     "Movie":"xxxxx",
     "Available":
        [             
            {
              "date":"31-08-2012",
              "timings":
                  [
                   "10:15",
                   "10:30",
                   "10:40"
                   ]
            },
            {
                "date":"1-09-2012",
                "timings":
                    [
                     "10:15",
                     "10:30",
                     "10:40"
                     ]
            }
        ]
}
]

This is the response i am getting. Can anyone suggest me how to convert the following Json Response to string in android and i want to display that data in listview in android native.

Community
  • 1
  • 1
code_finder
  • 1,370
  • 2
  • 21
  • 39
  • Converting to String or Object? – Bob Sep 10 '12 at 10:27
  • Your response from web service is an String. Is your problem converting that string to Object? – Bob Sep 10 '12 at 10:28
  • I think your question was not made properly. You want to know how to display JSON data in a listview, right? – Thiago M Rocha Sep 10 '12 at 10:35
  • -1 for lack of search http://stackoverflow.com/questions/3408985/json-array-iteration-in-android-java http://stackoverflow.com/questions/6277154/populate-listview-from-json – rds Sep 10 '12 at 10:41

2 Answers2

1

Check the link

It shows the full example to communicate with the server and get response in Json format.

Though in this PHP,MySql is used but I guess the json response you get is same for all languages.

Rishi
  • 330
  • 2
  • 13
0

look at this sample and convert per your need

ps: a json array is one which starts with [ and ends with ] a json object starts with{ and ends with } so in your case available is a json array and its 1 st object contains a json string date and another json array timings

JSONArray ja =jso.getJSONArray("Available");





            for (int i = 0; i < ja.length(); i++) {
                JSONObject jobj=ja.getJSONObject(i);
                xyz[i]=jobj.getString("date");
                abcd[i]=jobj.getJSONArray("timings").tostring;


            }


            }

edit:

    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
   JSONParser jParser = new JSONParser(); 
   JSONArray json = jParser.getJSONFromUrl(url);
   try { for (int i=0; i< json.length(); i++) {
    JSONObject details = json.getJSONObject(i);
    String id = details.getString("Id");
    String name = details.getString("Name");
    HashMap<String, String> map = new HashMap<String, String>(); 
   map.put(TAG_ID, id); map.put(TAG_NAME, name);


    contactList.add(map)
    JSONArray ja=details.getJSONArray("Available");

        for (int i = 0; i < ja.length(); i++) {
            JSONObject jobj=ja.getJSONObject(i);
           String a =jobj.getString("date");
          String b   =jobj.getJSONArray("timings").tostring;
    HashMap<String, String> map2 = new HashMap<String, String>(); 
   map2.put("a", a); map2.put("b", b);
    contactList.add(map2)

        }

     }
Athul Harikumar
  • 2,501
  • 18
  • 16
  • here as per my response "Available is an array which consists of objects, date & timings. Here again timings is an array of objects. " How to convert the following. Getting confusion with that. Can you please help me with this.... – code_finder Sep 10 '12 at 10:33
  • can you pls specify the reason of this down vote – Athul Harikumar Sep 10 '12 at 10:39
  • i am not the one down voted it even added a vote for your answer......... – code_finder Sep 10 '12 at 10:40
  • sorry i didnt mean that you down voted again you can reffer w3schools for a better understanding of json – Athul Harikumar Sep 10 '12 at 10:43
  • so here the whole json is an array and its 1 st object contains another array(availability) and two strings i hope you can coustomize as per your need from there on – Athul Harikumar Sep 10 '12 at 10:46
  • after a long search i added the following code " ArrayList> contactList = new ArrayList>(); JSONParser jParser = new JSONParser(); JSONArray json = jParser.getJSONFromUrl(url); try { for (int i=0; i< json.length(); i++) { JSONObject details = json.getJSONObject(i); String id = details.getString("Id"); String name = details.getString("Name"); HashMap map = new HashMap(); map.put(TAG_ID, id); map.put(TAG_NAME, name); contactList.add(map);" I am not getting how to add "AVAILABLE array". can you help me... – code_finder Sep 10 '12 at 10:48
  • k from there you shoyld add JSONArray ja=details.getJSONArray("availability"); and continue the steps you followed for getting string – Athul Harikumar Sep 10 '12 at 10:54
  • As per your code after converting to string, i am getting "timings" output as in the following manner "["10:15","10:30","10:40"]". How to get individually timings... like i want each time into separate string. String a = "10:15". String b = "10:30".. like this..... – code_finder Sep 10 '12 at 13:31
  • you can create another loop and extract those strings String b =jobj.getJSONArray("timings").tostring; change to JSONArray baaa =jobj.getJSONArray("timings"); for (int i = 0; i < baa.length(); i++) { String b=ja.getJSONObject(i).tostring;} and add to map – Athul Harikumar Sep 10 '12 at 13:41