码迷,mamicode.com
首页 > 移动开发 > 详细

安卓开发之json解析

时间:2016-07-12 11:45:09      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

1、从网页获取json返回字符串
 public class ReadNet extends AsyncTask<URL, Integer, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected String doInBackground(URL... params) {
            StringBuffer sb = new StringBuffer();
            try {
                HttpURLConnection connection = (HttpURLConnection) params[0].openConnection();
                InputStream is = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String readLine;
                while ((readLine = reader.readLine()) != null) {
                    sb.append(readLine);
                    sb.append("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return sb.toString();//得到json
        }

        @Override
        protected void onPostExecute(String s) {

            Document doc = Jsoup.parse(s);
            showArticle.setText(doc.toString());
            super.onPostExecute(s);
        }
    }
2、用JSONObject类和JSONArray类解析json字符串
 JSONObject jsonObject = new JSONObject(jsonString);//{}
 JSONArray jsonArray = new JSONArray(jsonString);//[{1},{2}]
 JSONObject jsonObject = jsonArray.optJSONObject(1);
 解析的可以放在list里面

安卓开发之json解析

标签:

原文地址:http://www.cnblogs.com/judes/p/5662908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!