• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
成为自己最想成为的那种人
博客园    首页    新随笔    联系   管理    订阅  订阅
使用Newtonsoft.Json将数据导出至Json文件

导出方法:

        /// <summary>
        /// 导出文件
        /// </summary>
        public void Export(List<Dict> dicts, string filepath)
        {
            File.WriteAllText(filepath, JsonConvert.SerializeObject(dicts, Formatting.Indented));
        }

调用:

            var coll = Mongodb.XXX.GetCollection<Dict>("dict");
            string[] arr = new string[] { "major", "industry", "post_type" };           //专业、行业、职位类别

            var data = await coll.Find(d => arr.Contains(d.dict_type)).ToListAsync().ConfigureAwait(false);

            //创建文件夹
            string path = "D:\\jsonfile";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            try
            {
                Export(data.Where(d => d.dict_type == arr[0]).ToList(), $"{path}\\{arr[0]}.json");
                Export(data.Where(d => d.dict_type == arr[1]).ToList(), $"{path}\\{arr[1]}.json");
                Export(data.Where(d => d.dict_type == arr[2]).ToList(), $"{path}\\{arr[2]}.json");
            }
            catch(Exception e)
            {
                Console.WriteLine($"{e.Message}:{e.ToString()}");
            }

 

posted on 2019-12-09 11:39  遇见未来  阅读(1099)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3