最全省份城市生成代码
1、进入国家统计局官网 查找最新省市区数据
http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t20170310_1471429.html
2、将数据放到txt文件,然后我们可以看到省份都是以 0000 结尾,市都是以 00 结尾,转为Json代码如下
class Program { static void Main(string[] args) { var provinces = new List<Province>(); var citys = File.ReadAllLines("city.txt",Encoding.UTF8); foreach (var c in citys) { var newc = c.Trim(); newc = new System.Text.RegularExpressions.Regex("[\\s]+").Replace(newc, " "); var citySplit = newc.Split(' '); if (citySplit.Length==2) { //如果以 0000 结尾就是省份 if (citySplit[0].EndsWith("0000")) { provinces.Add(new Province(){Name = citySplit[1],CityList = new List<CityInfo>()}); }else if (citySplit[0].EndsWith("00")) { provinces.Last().CityList.Add(new CityInfo(){Name = citySplit[1],AreaInfo = new List<string>()}); } else { provinces.Last().CityList.Last().AreaInfo.Add(citySplit[1]); } } } var jsonProvince = JsonConvert.SerializeObject(provinces); Console.ReadLine(); } } public class Province { [JsonProperty("name")] public string Name { get; set; } [JsonProperty("city")] public List<CityInfo> CityList { get; set; } } public class CityInfo { [JsonProperty("name")] public string Name { get; set; } [JsonProperty("area")] public List<string> AreaInfo { get; set; } }