現在通過json和gson來實現完成一個級聯下拉式清單方塊

來源:互聯網
上載者:User

標籤:json   gson   

現在通過json和gson來實現完成一個級聯下拉式清單方塊(例如:省市聯動)

1、前台jsp頁面:

<select name="address.upLeverId" id="Townside1"><span style="white-space:pre"></span><option value="-1">請選擇</option>                <s:iterator value="#request.addresses" id="area">                <option value="${id}" >${addressName}</option>                </s:iterator>    </select>   <select name="address.id" id="thecountrysides1">            </select>
2、前台js實現:

<script type="text/javascript" src="<%=path%>/js/jquery-1.9.1.min.js"> </script><pre name="code" class="java"><script type="text/javascript" >$(document).ready(function(){/** * 選擇地區 */$("#Townside1").change(function(){var $countrysides = $("#thecountrysides1");var $this = $(this);$.ajax( {type : "POST",url: "Commodity_getcountrysides.do",data:"address.id="+$this.val(),dataType : "json",contentType : "application/x-www-form-urlencoded ; charset=UTF-8",// 解決傳遞中文亂碼的問題success : function(msg) {$countrysides.empty();   $countrysides.append("<option selected='selected' value='-1'>請選擇</option>");if(msg.status == 1){   //"全部"的"value"屬性值應為"",與js校正相對應   $.each(msg.countrysides,function(index,item){   $("<option>",{"value":item.id,"text":item.addressName}).appendTo($countrysides);   });}}});});});</script>



3、後台java實現代碼:

public void getcountrysides(){try {JSONObject jsonObject = new JSONObject();Gson gson = new Gson();if(null !=address && null!=address.getId()){//查詢地區List<DAddress> addresses= addressService.query(address.getId());(這個是根據前台傳來的第一級地區來擷取它下一級的資料)//返回瀏覽器if(null !=addresses && !addresses.isEmpty()){System.out.println("gson.toJson(addresses:"+gson.toJson(addresses));(通過這個方式可以看出集合資料,這個也是gson的一大優勢)jsonObject.accumulate("countrysides", gson.toJson(addresses));jsonObject.accumulate(RETRUEN_STATUS, SUCCESS_STATUS);}else{jsonObject.accumulate(RETRUEN_STATUS, ERROR_STATUS);}}else{jsonObject.accumulate(RETRUEN_STATUS, ERROR_STATUS);}System.out.println("jsonObject.toString():"+jsonObject.toString());outPrint(jsonObject.toString());(最鐘還是要傳輸json格式資料,前台可以很方便的通過msg擷取資料)} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}

PS:json、gson 兩者的區別和聯絡

1、 json是一種資料格式,便於資料轉送、儲存、交換
 gson是一種組件庫,可以把java對象資料轉換成json資料格式

 Gson可以直接轉化成String[]數組,同時轉化OperConditions時也比json-lib簡單。

2、GSON簡單處理JSON
json格式經常需要用到,google提供了一個處理json的項目:GSON,能很方便的處理轉換java對象和JSON表達。他不需要使用annotation,也不需要對象的原始碼就能使用。
以字串為例介紹:
(1)構造json 字串
例如 要傳送json格式的字串

String appID = req.getParameter("appID");String userID = req.getParameter("userID");Map map = new HashMap();map.put("appID", appID);map.put("userID", userID);Gson gson = new Gson();String state = gson.toJson(map);
(2)解析json字串
JsonParser jsonparer = new JsonParser();//初始化解析json格式的對象String state = req.getParameter("state");String appID = jsonparer.parse(state).getAsJsonObject().get("appID").getAsString();String userID = jsonparer.parse(state).getAsJsonObject().get("userID").getAsString();


現在通過json和gson來實現完成一個級聯下拉式清單方塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.