全国城市json数据保存数据库
- C#
- 2022/1/24 14:09:00
- 人已阅读
简介github里搜索来的json数据源,结构不变的话可通过本类保存入库
citys.json 数据源:https://github.com/wecatch/china_regions
using Jabil.Common; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Jabil.MainControl.Forms { public partial class Form7 : Form { IFreeSql fsql = FreeFactory.GetSingleton().GetService(); public Form7() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; } class MyCity { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public string parentCode { get; set; } public string provCode { get; set; } public string provName { get; set; } public string cityCode { get; set; } public string cityName { get; set; } public string regionCode { get; set; } public string regionName { get; set; } } private void button2_Click(object sender, EventArgs e) { var tbCount = fsql.Select<MyCity>().Count(); if (tbCount > 0) { richTextBox1.Text = "请先删表数据再重新保存结果!" + tbCount; return; } var jsonText = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "/data/area.json"); //JObject dataView = JsonConvert.DeserializeObject<JObject>(jsonText); var dataView = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonText).ToList(); //var areas = dataView.Children(); var provs = dataView.Where(d => d.Key == "86").FirstOrDefault().Value;//所有省份 var provList = JsonConvert.DeserializeObject<Dictionary<string, object>>(provs.ToString()).ToList(); var list = new List<MyCity>(); //先从1级省开始循环 foreach (var item in provList) { var provCode = item.Key; var provName = item.Value; var mycity = new MyCity() { provCode = item.Key, provName = item.Value.ToString(), }; list.Add(mycity); var citys = dataView.Where(d => d.Key == item.Key).FirstOrDefault().Value;//所有城市 var cityList = JsonConvert.DeserializeObject<Dictionary<string, object>>(citys.ToString()).ToList(); foreach (var item2 in cityList) { provCode = item2.Key; provName = item2.Value; mycity = new MyCity() { parentCode = item.Key, provCode = item.Key, provName = item.Value.ToString(), cityCode = item2.Key, cityName = item2.Value.ToString(), }; list.Add(mycity); var regions = dataView.Where(d => d.Key == item2.Key).FirstOrDefault().Value;//所有区县 if (regions == null || string.IsNullOrEmpty(regions.ToString())) { continue; } var regionList = JsonConvert.DeserializeObject<Dictionary<string, object>>(regions.ToString()).ToList(); foreach (var item3 in regionList) { //provCode = item3.Key; provName = item3.Value; mycity = new MyCity() { parentCode = item2.Key, provCode = item.Key, provName = item.Value.ToString(), cityCode = item2.Key, cityName = item2.Value.ToString(), regionCode = item3.Key, regionName = item3.Value.ToString(), }; list.Add(mycity); } } } //File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "/data.txt", JsonConvert.SerializeObject(list)); var r = fsql.Insert(list).ExecuteAffrows(); richTextBox1.Text = r > 0 ? "添加成功" : "添加失败"; } } }
文章评论
- 消灭零回复