首页 > 移动平台 > 详细

安卓开发之json解析

时间:2016-07-12 11:43:04      阅读:227      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!