zoukankan      html  css  js  c++  java
  • C#读取,写入,保存json

    前提要引入: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;
            }
  • 相关阅读:
    异常机制和File类
    《JAVA程序设计》_第五周学习总结
    20165214 第五周学习任务
    20165214 第四周学习任务
    20165214 第三周学习任务
    20165214 预备作业3 Linux安装及学习
    20165313 预备作业3 Linux安装及学习
    学习基础和C语言基础调查
    20165313 我期望的师生关系
    int *ptr=(int *)(&a+1)
  • 原文地址:https://www.cnblogs.com/smartisn/p/15024110.html
Copyright © 2011-2022 走看看