[知了堂學習筆記]_JSON資料操作第2講(JSON的封裝與解析)

來源:互聯網
上載者:User

標籤:class   exce   解析   pos   trace   jsp   json格式   代碼   封裝   

上一講為大家講了什麼是JSON,那麼這一講為大家帶來了在WEB項目中JSON的用法,也就是JSON的封裝與解析。

此圖是資料庫中的部分內容

一、JSON封裝

所謂的JSON封裝,指的是在Servlet中將從資料庫得到的資料轉化成JSON格式的字串。

那麼什麼是JSON格式的字串?

  • JSON格式的字串就是指字串的內容要完全符合JSON資料的格式

這個就是在servlet中輸出的已經封裝好的JSON格式字串,在控制台輸出的效果。

如何封裝

  • 在我自己的WEB中,原本想用原生態的JSON去封裝,但是專案經理給我介紹了GSON,所以我是用GSON去封裝的
  •  

代碼(servlet)

    /***     * 通過id來檢索申訴資訊並傳到頁面AJAX調用     * admin     * @return     */    public String getListAppeal(HttpServletRequest request, HttpServletResponse response){        String id = request.getParameter("appeal_id");        try {            response.setContentType("text/html;charset=utf-8");            PrintWriter out = response.getWriter();            Gson gson = new Gson();            System.out.println("我是JSON格式字串:"+gson.toJson(appealService.getListAppeal(id)));            out.print(gson.toJson(appealService.getListAppeal(id)));            out.close();        } catch (IOException e) {            e.printStackTrace();        }        return null;//null代表AJAX調用            }

  • appealService.getListAppeal(id)
  1. appealService是service層的實作類別,主要去調用DAO進行JBDC
  2. getListAppeal(id) 如

  • gson.toJson()

後面的參數是Object ,那麼參數就可以是list,map,bean(自訂類)等。用這個方法會返回JSON封裝的字串

二、解析

JSON解析指JSP頁面接收到由Servlet發送的JSON字串,轉化為JSON對象的過程。

  • 在解析中我們需要使用到eval()這個方法

代碼:

$.ajax({                url:"appeal.do",                data:{"method":"getListAppeal","appeal_id":id},                datatype:"json",                type:"POST",                success: function(result){//result-->接收servlet層返回的參數                    console.log("未處理==="+result);                    var json = eval(result);/* JSON解析 */                    console.log("已處理==="+json);                    //擷取JSON資料中的值                    $("#id").val(json[0].appeal_id);                    $("#userAccount").val(json[0].user_account);                    $("#type").val(json[0].appeal_type);                    $("#content").val(json[0].appeal_content);                    $("#time").val(json[0].appeal_time);                }            });

結果:

值得提醒的一點是,在調用gson.toJson()方式,如果參數是MAP可能會出現一些問題,就是在解析過程中,按照上面的步驟不能轉化為JSON對象哦。

代碼:

$.ajax({                url:"orderServlet.do?method=getMapOrderNumByGame",                datatype:"JSON",                type:"POST",                async: false,                 success:function(result){                    /* alert(eval(result)) */                    console.log("未處理==》"+result);                    console.log("處理一次===》"+eval(result));                    var json = eval(eval(result));                    console.log("處理兩次===》"+json);                    option.series[0].data = json                    var s="";                    for ( var j in json) {                        s+=","+json[j].name                    }                    s=s.substr(1);                    option.legend.data=s.split(",");                    myChart.setOption(option);                }            });

結果:

可以看出,在未處理json字串的時候,該字串有存在逸出字元的,在處理一次後,就和上面的步驟一樣了,所以在這裡要進行兩次解析。

 

以上為今天的內容,如需瞭解更加深入的知識,請大家進入知了堂社區:http://www.zhiliaotang.com/portal.php;

 

[知了堂學習筆記]_JSON資料操作第2講(JSON的封裝與解析)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.