转载请注明出处:http://www.cnblogs.com/shamoyuu/p/element_cascader.htmlhtml
不想本身处理的就直接下载吧node
http://shamoyuu.bj.bcebos.com/DontDelete/citys.jsonelement-ui
效果图json
上一章咱们学会了用nodejs处理文件,这一章咱们就把数据处理成element-ui要求的格式,并保存到temp.txt文件ui
上代码spa
var fs = require('fs'); fs.readFile('out.json', function(err, data) { if (err) { console.log('文件读取失败'); } else { console.log('文件读取成功'); let citys = eval('(' + data + ')'); let province = []; // 全部省的jons,固定100000 let provinceJson = citys['100000']; let provinceArr = []; for(let provinceKey in provinceJson) { let cityJson = citys[provinceKey]; console.info(provinceKey) let cityArr = []; for(let cityKey in cityJson) { let quJson = citys[cityKey]; let quArr = []; for(let qukey in quJson) { quArr.push({ value: qukey, label: quJson[qukey] }); } if(quArr.length == 0) { console.info(cityKey, cityJson[cityKey], "没有区"); quArr.push({ value: cityKey, label: cityJson[cityKey] }); } cityArr.push({ value: cityKey, label: cityJson[cityKey], children: quArr }); } if(cityArr.length == 0) { console.info(provinceKey, provinceJson[provinceKey], "没有市"); cityArr.push({ value: provinceKey, label: provinceJson[provinceKey], children: [{ value: provinceKey, label: provinceJson[provinceKey], }] }); } provinceArr.push({ value: provinceKey, label: provinceJson[provinceKey], children: cityArr }); } fs.writeFile('temp.txt', JSON.stringify(provinceArr), function(err) { if (err) { console.log('文件写入失败'); } else { console.log('文件写入成功'); } }); } });
而后用nodejs运行这个js,就能够获得咱们想要的数据了code
完结,散花htm