【问题标题】:sample code to parse data through JSON in android [closed]在android中通过JSON解析数据的示例代码[关闭]
【发布时间】:2010-07-27 06:04:57
【问题描述】:

我想在 JSON 的帮助下解析 REST Web 服务。请通过提供示例代码帮助我如何通过 JSON 解析 android 中的其余 webservices xml

【问题讨论】:

    标签: android json


    【解决方案1】:

    这里是一个 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;
    }
    

    【讨论】:

      【解决方案2】:

      我刚刚实现了....

      Step1:创建一个主类Activity..

      类主{ ///调用方法parse(); ParserHelper ob=new ParseHelper(this,Arraylist from parse()) }

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

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多