首页 > Windows开发 > 详细

C#读取,写入,保存json

时间:2021-07-17 18:26:49      阅读:16      评论:0      收藏:0      [点我收藏+]

前提要引入:using Newtonsoft.Json;

读取:

        public static object Read_json(string Path)
        {
            Object obji = JsonConvert.DeserializeObject<Object>(File.ReadAllText(Path));  // 尖括号<>中填入对象的类名 
            return obji;
        }

写入:

 1  List<JsonModel> json_data = new List<JsonModel>();
 2             foreach (var method in Value_methods)
 3             {//构建字典
 4                 Console.WriteLine("*************************");
 5                 Console.WriteLine(method.Identifier.ValueText);
 6                 //Console.WriteLine(method.ParameterList);
 7                 //Console.WriteLine(method.ToFullString());
 8                 SyntaxTree tree_method = CSharpSyntaxTree.ParseText(method.ToFullString());
 9                 CompilationUnitSyntax root_method = tree_method.GetCompilationUnitRoot();
10                 var nodes = root_method.DescendantNodes()
11                             .OfType<MemberAccessExpressionSyntax>();                
12                 foreach (var node in nodes.Distinct())
13                 {
14                     JsonModel JsonValue = new JsonModel();
15                     JsonValue.Source = method.Identifier.ValueText;
16                     JsonValue.Target = node + "";
17                     json_data.Add(JsonValue);
18                 }
19             }
20             string json = JsonConvert.SerializeObject(json_data);

存储:

        public static Boolean Save_json(string Path,string value)
        {
            if (!File.Exists(Path))  // 判断是否已有相同文件 
            {
                FileStream fs1 = new FileStream(Path, FileMode.Create, FileAccess.ReadWrite);
                fs1.Close();
            }
            File.WriteAllText(Path, value);
            return true;
        }

 

C#读取,写入,保存json

原文:https://www.cnblogs.com/smartisn/p/15024110.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!