Now complete a cascading drop-down list box with JSON and Gson

Source: Internet
Author: User
Tags tojson

Now through the JSON and Gson to achieve a cascading drop-down list box (for example: provinces and cities linkage)

1. Front JSP page:

<select name= "Address.upleverid" id= "Townside1" ><span style= "White-space:pre" ></span><option Value= "-1" > Please select </option>                <s:iterator value= "#request. Addresses" id= "area" >                <option Value = "${id}" >${addressName}</option>                </s:iterator>    </select>   <select name= " Address.id "id=" thecountrysides1 ">            </select>
2, the front desk JS implementation:

<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 () {/** * Select region */$ (" #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 ",//solve the problem of passing Chinese garbled characters success:function (msg) {$countrysides. empty ();   $countrysides. Append ("<option selected= ' selected ' value= '-1 ' > Please select </option>"); if (msg.status = = 1) {   / /"All" Value property should be "", corresponding to JS checksum   $.each (msg.countrysides,function (index,item) {   $ ("<option>", {"Value ": Item.id," text ": Item.addressname}). AppendTo ($countrysides);});});});   </script>


3, the background Java implementation code:

public void Getcountrysides () {try {jsonobject jsonobject = new Jsonobject (); Gson Gson = new Gson (), if (null!=address && Null!=address.getid ()) {//query region list<daddress> addresses= Addressservice.query (Address.getid ()); (This is based on the first level of the reception area to get its next level of data)//return to the browser if (null!=addresses &&!) Addresses.isempty ()) {System.out.println ("Gson.tojson (Addresses:" +gson.tojson (addresses)); (In this way you can see the collection data, This is also a big advantage of 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 ()); (The most important thing is to transfer JSON format data, the foreground can easily get the data via msg)} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();}}

The difference and connection between Ps:json and Gson

1, JSON is a data format for easy data transmission, storage, exchange
Gson is a library of components that convert Java object data to JSON data formats

Gson can be converted directly into string[] arrays, while converting operconditions is simpler than json-lib.

2, Gson simple processing JSON
The JSON format is often used, and Google provides a project for working with JSON: Gson, which can easily handle translating Java objects and JSON representations. He does not need to use annotation, and does not need the source code of the object to be able to use.
Take the string as an example to introduce:
(1) Constructing a JSON string
For example, to transfer a JSON-formatted string

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) Parsing JSON strings
Jsonparser jsonparer = new Jsonparser ();//Initialize the object in JSON format parsing string state = Req.getparameter ("state"); String AppID = Jsonparer.parse (state). Getasjsonobject (). Get ("AppID"). Getasstring (); String UserID = Jsonparer.parse (state). Getasjsonobject (). Get ("UserID"). Getasstring ();


Now complete a cascading drop-down list box with JSON and Gson

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.