示例代码通过android中的JSON解析数据

时间:2010-07-27 06:04:58

标签: android json

我想在JSON的帮助下解析REST Webservices。 Plz通过提供示例代码帮助我如何通过JSON

解析android中的其余webservices xml

2 个答案:

答案 0 :(得分:4)

以下是JSON解析的示例,如果它是您需要的,用于解析包含联系人的JSON文件:

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

答案 1 :(得分:2)

我刚才有了...... ....

步骤1:创建主类活动..

class main { /// call方法解析(); ParserHelper ob = new ParseHelper(this,来自parse()的Arraylist) }

public ArrayList<String> parse(){
    ArrayList<String> listItems = new ArrayList<String>();
    try{
        URL twitter = new URL("http://twitter.com/statuses/public_timeline.json");
        URLConnection tc = twitter.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

        String line;
        while ((line = in.readLine()) != null) 
        {
            JSONArray ja = new JSONArray(line);
            for (int i = 0; i < ja.length(); i++) 
            {
                JSONObject jo = (JSONObject) ja.get(i);
                if(jo.getString("text")!=null)
                listItems.add(jo.getString("text"));
            }
        }
    } catch (MalformedURLException e) 
    {

        e.printStackTrace();
    } 

步骤:2创建类以将其膨胀到主列表 公共类JsonHelper扩展了BaseAdapter {

Activity a;
ArrayList<String> list=new ArrayList<String>();

private static LayoutInflater inflate=null;

JsonHelper(Activity a,ArrayList<String> list){
    this.a=a;
    this.list=list;
    inflate=(LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    list.size();
}
@Override
public int getCount() {

    return list.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

private class ViewHolder{
    TextView txt1;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   View v=convertView;
   ViewHolder vh;

   if(convertView==null){
       v=inflate.inflate(R.layout.simplelistitem,null);
    vh=new ViewHolder();
    vh.txt1=(TextView) v.findViewById(R.id.text_view);
    v.setTag(vh);
   }
   else{
  vh=(ViewHolder) v.getTag();
   }
   vh.txt1.setText(list.get(position).toString());
   return v;
}

}