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;
            }
  • 相关阅读:
    MTK 关闭耳机调至最大音量时,提示损伤听力
    MTK LCM的添加
    chmod chown
    ubuntu14.04设置静态IP
    MTK NTP和NITZ更新时间的问题
    Rk3288 双屏异显单触摸
    MTK 修改默认时区
    MTK 屏幕旋转90度
    MTK WIFI底部加入返回按钮
    MTK 自定义按键添加广播
  • 原文地址:https://www.cnblogs.com/smartisn/p/15024110.html
Copyright © 2011-2022 走看看