前段时间做项目,需要把全国省市的两个XML文件整合成一个JSON格式的数据,手写的话觉得数据太多了,而且容易出错,于是就想到了用Ajax嵌套的方法来解决,就想平时用Ajax的方法直接嵌套,都会先读出外面Ajax的内容,然后才读取嵌套在Ajax里面的内容,后面经网上查找资料,加个async:false这个Ajax参数就行了,下面是贴代码:
<script type="text/javascript"> $(function () { $.ajax({ type: "get", url: "/XmlData/Provinces.xml", datatype: "xml", success: function (xml) { $("body").append("{"); $(xml).find("Provinces > Province").each(function (i) { $("body").append("{"PID":"" + $(this).attr("ID") + "","ProvinceName": "" + $(this).attr("ProvinceName") + "","Citys":["); //根据省份,请求读取该省的市 GetCitys($(this).attr("ID")); //第二个请求end $("body").append("]},<br/>"); }); } }); }) //第二次请求 function GetCitys(pid) { $.ajax({ type: "get", url: "/XmlData/Cities.xml", datatype: "xml", async: false,//注意!!这个为必要的参数,不然这个会在上面的Ajax请求后才执行 success: function (xml) { $(xml).find("Cities > City[PID=" + pid + "]").each(function (i) { $("body").append("{"CityID":"" + $(this).attr("ID") + "","CityName":"" + $(this).attr("CityName") + ""},"); }); } }); } </script>
记得在前面加上jq库,
下面把得到的全国省市的JSON数据贴出来,希望以后能用到:
{{"ProvinceID":"1","Province":"北京市","City":[{"CityID":"1","CityName":"北京市"}]}, {"ProvinceID":"2","Province":"天津市","City":[{"CityID":"2","CityName":"天津市"}]}, {"ProvinceID":"3","Province":"河北省","City":[{"CityID":"3","CityName":"石家庄市"},{"CityID":"4","CityName":"唐山市"},{"CityID":"5","CityName":"秦皇岛市"},{"CityID":"6","CityName":"邯郸市"},{"CityID":"7","CityName":"邢台市"},{"CityID":"8","CityName":"保定市"},{"CityID":"9","CityName":"张家口市"},{"CityID":"10","CityName":"承德市"},{"CityID":"11","CityName":"沧州市"},{"CityID":"12","CityName":"廊坊市"},{"CityID":"13","CityName":"衡水市"}]}, {"ProvinceID":"4","Province":"山西省","City":[{"CityID":"14","CityName":"太原市"},{"CityID":"15","CityName":"大同市"},{"CityID":"16","CityName":"阳泉市"},{"CityID":"17","CityName":"长治市"},{"CityID":"18","CityName":"晋城市"},{"CityID":"19","CityName":"朔州市"},{"CityID":"20","CityName":"晋中市"},{"CityID":"21","CityName":"运城市"},{"CityID":"22","CityName":"忻州市"},{"CityID":"23","CityName":"临汾市"},{"CityID":"24","CityName":"吕梁市"}]}, {"ProvinceID":"5","Province":"内蒙古自治区","City":[{"CityID":"25","CityName":"呼和浩特市"},{"CityID":"26","CityName":"包头市"},{"CityID":"27","CityName":"乌海市"},{"CityID":"28","CityName":"赤峰市"},{"CityID":"29","CityName":"通辽市"},{"CityID":"30","CityName":"鄂尔多斯市"},{"CityID":"31","CityName":"呼伦贝尔市"},{"CityID":"32","CityName":"巴彦淖尔市"},{"CityID":"33","CityName":"乌兰察布市"},{"CityID":"34","CityName":"兴安盟"},{"CityID":"35","CityName":"锡林郭勒盟"},{"CityID":"36","CityName":"阿拉善盟"}]}, {"ProvinceID":"6","Province":"辽宁省","City":[{"CityID":"37","CityName":"沈阳市"},{"CityID":"38","CityName":"大连市"},{"CityID":"39","CityName":"鞍山市"},{"CityID":"40","CityName":"抚顺市"},{"CityID":"41","CityName":"本溪市"},{"CityID":"42","CityName":"丹东市"},{"CityID":"43","CityName":"锦州市"},{"CityID":"44","CityName":"营口市"},{"CityID":"45","CityName":"阜新市"},{"CityID":"46","CityName":"辽阳市"},{"CityID":"47","CityName":"盘锦市"},{"CityID":"48","CityName":"铁岭市"},{"CityID":"49","CityName":"朝阳市"},{"CityID":"50","CityName":"葫芦岛市"}]}, {"ProvinceID":"7","Province":"吉林省","City":[{"CityID":"51","CityName":"长春市"},{"CityID":"52","CityName":"吉林市"},{"CityID":"53","CityName":"四平市"},{"CityID":"54","CityName":"辽源市"},{"CityID":"55","CityName":"通化市"},{"CityID":"56","CityName":"白山市"},{"CityID":"57","CityName":"松原市"},{"CityID":"58","CityName":"白城市"},{"CityID":"59","CityName":"延边朝鲜族自治州"}]}, {"ProvinceID":"8","Province":"黑龙江省","City":[{"CityID":"60","CityName":"哈尔滨市"},{"CityID":"61","CityName":"齐齐哈尔市"},{"CityID":"62","CityName":"鸡西市"},{"CityID":"63","CityName":"鹤岗市"},{"CityID":"64","CityName":"双鸭山市"},{"CityID":"65","CityName":"大庆市"},{"CityID":"66","CityName":"伊春市"},{"CityID":"67","CityName":"佳木斯市"},{"CityID":"68","CityName":"七台河市"},{"CityID":"69","CityName":"牡丹江市"},{"CityID":"70","CityName":"黑河市"},{"CityID":"71","CityName":"绥化市"},{"CityID":"72","CityName":"大兴安岭地区"}]}, {"ProvinceID":"9","Province":"上海市","City":[{"CityID":"73","CityName":"上海市"}]}, {"ProvinceID":"10","Province":"江苏省","City":[{"CityID":"74","CityName":"南京市"},{"CityID":"75","CityName":"无锡市"},{"CityID":"76","CityName":"徐州市"},{"CityID":"77","CityName":"常州市"},{"CityID":"78","CityName":"苏州市"},{"CityID":"79","CityName":"南通市"},{"CityID":"80","CityName":"连云港市"},{"CityID":"81","CityName":"淮安市"},{"CityID":"82","CityName":"盐城市"},{"CityID":"83","CityName":"扬州市"},{"CityID":"84","CityName":"镇江市"},{"CityID":"85","CityName":"泰州市"},{"CityID":"86","CityName":"宿迁市"}]}, {"ProvinceID":"11","Province":"浙江省","City":[{"CityID":"87","CityName":"杭州市"},{"CityID":"88","CityName":"宁波市"},{"CityID":"89","CityName":"温州市"},{"CityID":"90","CityName":"嘉兴市"},{"CityID":"91","CityName":"湖州市"},{"CityID":"92","CityName":"绍兴市"},{"CityID":"93","CityName":"金华市"},{"CityID":"94","CityName":"衢州市"},{"CityID":"95","CityName":"舟山市"},{"CityID":"96","CityName":"台州市"},{"CityID":"97","CityName":"丽水市"}]}, {"ProvinceID":"12","Province":"安徽省","City":[{"CityID":"98","CityName":"合肥市"},{"CityID":"99","CityName":"芜湖市"},{"CityID":"100","CityName":"蚌埠市"},{"CityID":"101","CityName":"淮南市"},{"CityID":"102","CityName":"马鞍山市"},{"CityID":"103","CityName":"淮北市"},{"CityID":"104","CityName":"铜陵市"},{"CityID":"105","CityName":"安庆市"},{"CityID":"106","CityName":"黄山市"},{"CityID":"107","CityName":"滁州市"},{"CityID":"108","CityName":"阜阳市"},{"CityID":"109","CityName":"宿州市"},{"CityID":"110","CityName":"巢湖市"},{"CityID":"111","CityName":"六安市"},{"CityID":"112","CityName":"亳州市"},{"CityID":"113","CityName":"池州市"},{"CityID":"114","CityName":"宣城市"}]}, {"ProvinceID":"13","Province":"福建省","City":[{"CityID":"115","CityName":"福州市"},{"CityID":"116","CityName":"厦门市"},{"CityID":"117","CityName":"莆田市"},{"CityID":"118","CityName":"三明市"},{"CityID":"119","CityName":"泉州市"},{"CityID":"120","CityName":"漳州市"},{"CityID":"121","CityName":"南平市"},{"CityID":"122","CityName":"龙岩市"},{"CityID":"123","CityName":"宁德市"}]}, {"ProvinceID":"14","Province":"江西省","City":[{"CityID":"124","CityName":"南昌市"},{"CityID":"125","CityName":"景德镇市"},{"CityID":"126","CityName":"萍乡市"},{"CityID":"127","CityName":"九江市"},{"CityID":"128","CityName":"新余市"},{"CityID":"129","CityName":"鹰潭市"},{"CityID":"130","CityName":"赣州市"},{"CityID":"131","CityName":"吉安市"},{"CityID":"132","CityName":"宜春市"},{"CityID":"133","CityName":"抚州市"},{"CityID":"134","CityName":"上饶市"}]}, {"ProvinceID":"15","Province":"山东省","City":[{"CityID":"135","CityName":"济南市"},{"CityID":"136","CityName":"青岛市"},{"CityID":"137","CityName":"淄博市"},{"CityID":"138","CityName":"枣庄市"},{"CityID":"139","CityName":"东营市"},{"CityID":"140","CityName":"烟台市"},{"CityID":"141","CityName":"潍坊市"},{"CityID":"142","CityName":"济宁市"},{"CityID":"143","CityName":"泰安市"},{"CityID":"144","CityName":"威海市"},{"CityID":"145","CityName":"日照市"},{"CityID":"146","CityName":"莱芜市"},{"CityID":"147","CityName":"临沂市"},{"CityID":"148","CityName":"德州市"},{"CityID":"149","CityName":"聊城市"},{"CityID":"150","CityName":"滨州市"},{"CityID":"151","CityName":"荷泽市"}]}, {"ProvinceID":"16","Province":"河南省","City":[{"CityID":"152","CityName":"郑州市"},{"CityID":"153","CityName":"开封市"},{"CityID":"154","CityName":"洛阳市"},{"CityID":"155","CityName":"平顶山市"},{"CityID":"156","CityName":"安阳市"},{"CityID":"157","CityName":"鹤壁市"},{"CityID":"158","CityName":"新乡市"},{"CityID":"159","CityName":"焦作市"},{"CityID":"160","CityName":"濮阳市"},{"CityID":"161","CityName":"许昌市"},{"CityID":"162","CityName":"漯河市"},{"CityID":"163","CityName":"三门峡市"},{"CityID":"164","CityName":"南阳市"},{"CityID":"165","CityName":"商丘市"},{"CityID":"166","CityName":"信阳市"},{"CityID":"167","CityName":"周口市"},{"CityID":"168","CityName":"驻马店市"}]}, {"ProvinceID":"17","Province":"湖北省","City":[{"CityID":"169","CityName":"武汉市"},{"CityID":"170","CityName":"黄石市"},{"CityID":"171","CityName":"十堰市"},{"CityID":"172","CityName":"宜昌市"},{"CityID":"173","CityName":"襄樊市"},{"CityID":"174","CityName":"鄂州市"},{"CityID":"175","CityName":"荆门市"},{"CityID":"176","CityName":"孝感市"},{"CityID":"177","CityName":"荆州市"},{"CityID":"178","CityName":"黄冈市"},{"CityID":"179","CityName":"咸宁市"},{"CityID":"180","CityName":"随州市"},{"CityID":"181","CityName":"恩施土家族苗族自治州"},{"CityID":"182","CityName":"神农架"}]}, {"ProvinceID":"18","Province":"湖南省","City":[{"CityID":"183","CityName":"长沙市"},{"CityID":"184","CityName":"株洲市"},{"CityID":"185","CityName":"湘潭市"},{"CityID":"186","CityName":"衡阳市"},{"CityID":"187","CityName":"邵阳市"},{"CityID":"188","CityName":"岳阳市"},{"CityID":"189","CityName":"常德市"},{"CityID":"190","CityName":"张家界市"},{"CityID":"191","CityName":"益阳市"},{"CityID":"192","CityName":"郴州市"},{"CityID":"193","CityName":"永州市"},{"CityID":"194","CityName":"怀化市"},{"CityID":"195","CityName":"娄底市"},{"CityID":"196","CityName":"湘西土家族苗族自治州"}]}, {"ProvinceID":"19","Province":"广东省","City":[{"CityID":"197","CityName":"广州市"},{"CityID":"198","CityName":"韶关市"},{"CityID":"199","CityName":"深圳市"},{"CityID":"200","CityName":"珠海市"},{"CityID":"201","CityName":"汕头市"},{"CityID":"202","CityName":"佛山市"},{"CityID":"203","CityName":"江门市"},{"CityID":"204","CityName":"湛江市"},{"CityID":"205","CityName":"茂名市"},{"CityID":"206","CityName":"肇庆市"},{"CityID":"207","CityName":"惠州市"},{"CityID":"208","CityName":"梅州市"},{"CityID":"209","CityName":"汕尾市"},{"CityID":"210","CityName":"河源市"},{"CityID":"211","CityName":"阳江市"},{"CityID":"212","CityName":"清远市"},{"CityID":"213","CityName":"东莞市"},{"CityID":"214","CityName":"中山市"},{"CityID":"215","CityName":"潮州市"},{"CityID":"216","CityName":"揭阳市"},{"CityID":"217","CityName":"云浮市"}]}, {"ProvinceID":"20","Province":"广西壮族自治区","City":[{"CityID":"218","CityName":"南宁市"},{"CityID":"219","CityName":"柳州市"},{"CityID":"220","CityName":"桂林市"},{"CityID":"221","CityName":"梧州市"},{"CityID":"222","CityName":"北海市"},{"CityID":"223","CityName":"防城港市"},{"CityID":"224","CityName":"钦州市"},{"CityID":"225","CityName":"贵港市"},{"CityID":"226","CityName":"玉林市"},{"CityID":"227","CityName":"百色市"},{"CityID":"228","CityName":"贺州市"},{"CityID":"229","CityName":"河池市"},{"CityID":"230","CityName":"来宾市"},{"CityID":"231","CityName":"崇左市"}]}, {"ProvinceID":"21","Province":"海南省","City":[{"CityID":"232","CityName":"海口市"},{"CityID":"233","CityName":"三亚市"}]}, {"ProvinceID":"22","Province":"重庆市","City":[{"CityID":"234","CityName":"重庆市"}]}, {"ProvinceID":"23","Province":"四川省","City":[{"CityID":"235","CityName":"成都市"},{"CityID":"236","CityName":"自贡市"},{"CityID":"237","CityName":"攀枝花市"},{"CityID":"238","CityName":"泸州市"},{"CityID":"239","CityName":"德阳市"},{"CityID":"240","CityName":"绵阳市"},{"CityID":"241","CityName":"广元市"},{"CityID":"242","CityName":"遂宁市"},{"CityID":"243","CityName":"内江市"},{"CityID":"244","CityName":"乐山市"},{"CityID":"245","CityName":"南充市"},{"CityID":"246","CityName":"眉山市"},{"CityID":"247","CityName":"宜宾市"},{"CityID":"248","CityName":"广安市"},{"CityID":"249","CityName":"达州市"},{"CityID":"250","CityName":"雅安市"},{"CityID":"251","CityName":"巴中市"},{"CityID":"252","CityName":"资阳市"},{"CityID":"253","CityName":"阿坝藏族羌族自治州"},{"CityID":"254","CityName":"甘孜藏族自治州"},{"CityID":"255","CityName":"凉山彝族自治州"}]}, {"ProvinceID":"24","Province":"贵州省","City":[{"CityID":"256","CityName":"贵阳市"},{"CityID":"257","CityName":"六盘水市"},{"CityID":"258","CityName":"遵义市"},{"CityID":"259","CityName":"安顺市"},{"CityID":"260","CityName":"铜仁地区"},{"CityID":"261","CityName":"黔西南布依族苗族自治州"},{"CityID":"262","CityName":"毕节地区"},{"CityID":"263","CityName":"黔东南苗族侗族自治州"},{"CityID":"264","CityName":"黔南布依族苗族自治州"}]}, {"ProvinceID":"25","Province":"云南省","City":[{"CityID":"265","CityName":"昆明市"},{"CityID":"266","CityName":"曲靖市"},{"CityID":"267","CityName":"玉溪市"},{"CityID":"268","CityName":"保山市"},{"CityID":"269","CityName":"昭通市"},{"CityID":"270","CityName":"丽江市"},{"CityID":"271","CityName":"思茅市"},{"CityID":"272","CityName":"临沧市"},{"CityID":"273","CityName":"楚雄彝族自治州"},{"CityID":"274","CityName":"红河哈尼族彝族自治州"},{"CityID":"275","CityName":"文山壮族苗族自治州"},{"CityID":"276","CityName":"西双版纳傣族自治州"},{"CityID":"277","CityName":"大理白族自治州"},{"CityID":"278","CityName":"德宏傣族景颇族自治州"},{"CityID":"279","CityName":"怒江傈僳族自治州"},{"CityID":"280","CityName":"迪庆藏族自治州"}]}, {"ProvinceID":"26","Province":"西藏自治区","City":[{"CityID":"281","CityName":"拉萨市"},{"CityID":"282","CityName":"昌都地区"},{"CityID":"283","CityName":"山南地区"},{"CityID":"284","CityName":"日喀则地区"},{"CityID":"285","CityName":"那曲地区"},{"CityID":"286","CityName":"阿里地区"},{"CityID":"287","CityName":"林芝地区"}]}, {"ProvinceID":"27","Province":"陕西省","City":[{"CityID":"288","CityName":"西安市"},{"CityID":"289","CityName":"铜川市"},{"CityID":"290","CityName":"宝鸡市"},{"CityID":"291","CityName":"咸阳市"},{"CityID":"292","CityName":"渭南市"},{"CityID":"293","CityName":"延安市"},{"CityID":"294","CityName":"汉中市"},{"CityID":"295","CityName":"榆林市"},{"CityID":"296","CityName":"安康市"},{"CityID":"297","CityName":"商洛市"}]}, {"ProvinceID":"28","Province":"甘肃省","City":[{"CityID":"298","CityName":"兰州市"},{"CityID":"299","CityName":"嘉峪关市"},{"CityID":"300","CityName":"金昌市"},{"CityID":"301","CityName":"白银市"},{"CityID":"302","CityName":"天水市"},{"CityID":"303","CityName":"武威市"},{"CityID":"304","CityName":"张掖市"},{"CityID":"305","CityName":"平凉市"},{"CityID":"306","CityName":"酒泉市"},{"CityID":"307","CityName":"庆阳市"},{"CityID":"308","CityName":"定西市"},{"CityID":"309","CityName":"陇南市"},{"CityID":"310","CityName":"临夏回族自治州"},{"CityID":"311","CityName":"甘南藏族自治州"}]}, {"ProvinceID":"29","Province":"青海省","City":[{"CityID":"312","CityName":"西宁市"},{"CityID":"313","CityName":"海东地区"},{"CityID":"314","CityName":"海北藏族自治州"},{"CityID":"315","CityName":"黄南藏族自治州"},{"CityID":"316","CityName":"海南藏族自治州"},{"CityID":"317","CityName":"果洛藏族自治州"},{"CityID":"318","CityName":"玉树藏族自治州"},{"CityID":"319","CityName":"海西蒙古族藏族自治州"}]}, {"ProvinceID":"30","Province":"宁夏回族自治区","City":[{"CityID":"320","CityName":"银川市"},{"CityID":"321","CityName":"石嘴山市"},{"CityID":"322","CityName":"吴忠市"},{"CityID":"323","CityName":"固原市"},{"CityID":"324","CityName":"中卫市"}]}, {"ProvinceID":"31","Province":"新疆维吾尔自治区","City":[{"CityID":"325","CityName":"乌鲁木齐市"},{"CityID":"326","CityName":"克拉玛依市"},{"CityID":"327","CityName":"吐鲁番地区"},{"CityID":"328","CityName":"哈密地区"},{"CityID":"329","CityName":"昌吉回族自治州"},{"CityID":"330","CityName":"博尔塔拉蒙古自治州"},{"CityID":"331","CityName":"巴音郭楞蒙古自治州"},{"CityID":"332","CityName":"阿克苏地区"},{"CityID":"333","CityName":"克孜勒苏柯尔克孜自治州"},{"CityID":"334","CityName":"喀什地区"},{"CityID":"335","CityName":"和田地区"},{"CityID":"336","CityName":"伊犁哈萨克自治州"},{"CityID":"337","CityName":"塔城地区"},{"CityID":"338","CityName":"阿勒泰地区"},{"CityID":"339","CityName":"石河子市"},{"CityID":"340","CityName":"阿拉尔市"},{"CityID":"341","CityName":"图木舒克市"},{"CityID":"342","CityName":"五家渠市"}]}, {"ProvinceID":"32","Province":"香港特别行政区","City":[{"CityID":"343","CityName":"香港特别行政区"}]}, {"ProvinceID":"33","Province":"澳门特别行政区","City":[{"CityID":"344","CityName":"澳门特别行政区"}]}, {"ProvinceID":"34","Province":"台湾省","City":[{"CityID":"345","CityName":"台湾省"}]}}