image.png
浏览器端访问
行政区域数据
$.get("/v3/config/district?subdistrict=2&key=xxx",
function (data, textStatus, jqXHR) {
console.log(data);
}
);
使用node生成json文件,转换为自己所需的格式
const axios = require('axios')
const fs = require("fs")
axios.get('/v3/config/district?subdistrict=2&key=xxx')
.then(function (res) {
console.log(res.data);
if (res.status == 200) {
const data = customData(res.data.districts[0].districts)
const jsonStr = JSON.stringify(data, "", "\t");
fs.writeFile('area.json', jsonStr, function (err) {
if (err) {
return console.error(err);
}
console.log("数据写入成功!");
});
}
})
.catch(function (error) {
console.log(error);
});
function customData(arr) {
let array = [];
get(arr, array)
return array;
function get(districts, children) {
districts.forEach(item => {
children.push({
value: item.adcode,
label: item.name
})
if (item.districts.length != 0) {
children[children.length - 1].children = []
get(item.districts, children[children.length - 1].children)
}
});
}
}