tags:

views:

13

answers:

1

I want to parse REST Webservices with the help of JSON. Plz help me by giving sample code how to parse the rest webservices xml in android through JSON

+1  A: 

Here is an example of JSON parsing if it is what you need, for parsing a JSON file containing contacts :

List<Contact> result = new ArrayList<Contact>();
    try {
        // Creation a the http client to connect to the url and get the json
        // file
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(urlContacts);
        HttpResponse httpResponse = httpclient.execute(httppost);
        // json file returned as a string
        jStringContacts = EntityUtils.toString(httpResponse.getEntity());
        jsonContacts = new JSONObject(jStringContacts);
        JSONObject contactsObject = jsonContacts.getJSONObject("contacts");
        JSONArray jsonContactsArray = contactsObject.getJSONArray("contact");
        // loop to extract one contact from the JSON Array
        for (int i = 0; i < jsonContactsArray.length(); i++) {
            jsonContact = jsonContactsArray.getJSONObject(i);
            Contact contact = new Contact();
            contact.firstname = jsonContact.getString("firstname");
            contact.lastname = jsonContact.getString("lastname");
            contact.iconName = jsonContact.getString("profileImageName");
            contact.isZenika = "1".equals(jsonContact.getString("zenika"));
            contact.biography = jsonContact.getString("shortBio");
            result.add(contact);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return result;
}
Sephy