js or jquery parsing json is something we often encounter in our work, but as a back-end engineer, you may know that js can parse json, but you often don’t remember it well, and record it here for your own or everyone’s reference, thank you!
For ajax, please refer to http://patronli.iteye.com/admin/blogs/2324171
jquery parses xml and splices it into the drop-down box http://patronli.iteye.com/blog/2383737
1. Background code (generates json array):
List<CityModel> cms=new ArrayList<>();//list数组 JSONArray ja=JSONArray.fromObject(citys);;//Convert the array to json array
2. Front-end code (js part)
var city=$("#city"); var info=[{"cid":1,"cname":"东城区"},{"cid":2,"cname":"西城区"}]; //Assume info is the json data passed by the background through ajax var json=eval(info);//Replace json string with parseable array for (var i = json.length - 1; i >= 0; i--) {//The beauty of the loop array is an object city.prepend("<option>"+ json[i].cname +"</option>");//拼接 };
3. Front-end code (jsp part)
<select id="city" name="city"> <option value="0">--请选择--</option> </select>
The attachment is the package required to generate json