样品code到Android中通过JSON解析数据 [英] sample code to parse data through JSON in android

查看:115
本文介绍了样品code到Android中通过JSON解析数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分析REST Web服务使用JSON的帮助。 plz帮助我,使样品code如何通过JSON解析剩下的XML Web服务中的Andr​​oid

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

推荐答案

下面是JSON解析的一个例子,如果它是你需要什么,解析包含联系人的JSON文件:

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;
}

这篇关于样品code到Android中通过JSON解析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆