zoukankan      html  css  js  c++  java
  • JSON封装与解析

    servlet端封装成json格式

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter out = response.getWriter();
            DBQueryImpl query = new DBQueryImpl();
            ArrayList<NoticeBean> _list = query.getNotice();<span style="white-space:pre">    </span>//返回一个NoticeBean类
            // 转为json格式
            // JSONArray array = JSONArray.fromObject(_list);
    
            JSONArray array = new JSONArray();
            array.addAll(_list);
            // 打印到网页上,不要打印方括号
    
            String str=array.toString();
            
            out.write(str);
    
            out.flush();
            out.close();
        }

    android端解析出来

    private void getJSONString() {
            String urlPath = "http://192.168.1.102:8080/epay_server/QueryNotice";
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(urlPath);
            try {
                InputStream responseStream = client.execute(post).getEntity()
                        .getContent();
                // 记得转换成gbk编码
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(responseStream, "UTF-8"));
                String webContentString = bufferedReader.readLine();
                System.out.println("webCont==>" + webContentString);
    
                // ItemParent item = new Gson().fromJson(webContentString,
                // ItemParent.class);
                // 生成类数组
                NoticeBean[] _notice = new Gson().fromJson(webContentString,<span style="white-space:pre">    </span>//自动解析成NoticeBean类
                        NoticeBean[].class);
                ArrayList<NoticeBean> _list = new ArrayList<NoticeBean>();
                for (NoticeBean noticeBean : _list) {
                    _list.add(noticeBean);
                }
    
    //            遍历list表
                for (int i = 0; i < _notice.length; i++) {
                    System.out.println(_notice[i].get_content());
                }
            } catch (Exception e) {
                System.out.println("解析失败!");
            }
        }
  • 相关阅读:
    程序员学习视频教程汇总
    分享一个单机软件试用期计时思路
    C# XML转Json Json转XML XML 转对象 对象转XML
    C# 数独 解法
    C# 炸弹人 winform版
    胃食管反流疾病认识总结
    C# Datagridview combox列 初始化颜色
    C# WPF 坦克大战
    高分辨率食道测压(HRM)
    C# wpf 使用 polyline 做一个贪吃蛇游戏的小蛇移动吃食部分功能
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5549460.html
Copyright © 2011-2022 走看看