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;
            }
  • 相关阅读:
    人脸识别员工考勤系统
    栈和队列
    线性表
    C语言博客作业02--循环结构
    课程设计-个人博客
    C博客作业02--循环结构
    博客作业--函数
    c博客作业
    联系方式
    专业特长
  • 原文地址:https://www.cnblogs.com/smartisn/p/15024110.html
Copyright © 2011-2022 走看看