C#利用newtonsoft.json读取.so配置文件内容
今天花 了点时间来使用 C#读取json文件 ,文件后缀为 .so文件 ,也是基于文件流的形式 获取 对象 ,然后解析;
之所以尝试 使用 json读取 ,是因为其配置文件的格式 更为友好 和方便,直观 且形象,当然 XML也是很方便的;
主要是多了一种读取 配置文件的方式;特记录下来,方便后续项目实际使用;
格式如图:
需要注意的是这种格式需注意编辑;
当然通过代码初始化和写入的话,会自动生成如上的格式的,本文只完成如何读取配置文件的信息;
引用的程序集如:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;
//获取 jobject的对象,及读取键值的方法
class JsonConfigHelper
{
public JObject jObject = null;
public string this[string key]
{ get
{
string str = "";
if (jObject != null)
{
str = GetValue(key);
}
return str;
}
}
public JsonConfigHelper(string path)
{
jObject = new JObject();
using (System.IO.StreamReader file = System.IO.File.OpenText(path))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
jObject = JObject.Load(reader);
}
};
}
public T GetValue<T>(string key) where T : class
{
return JsonConvert.DeserializeObject<T>(jObject.SelectToken(key).ToString());
}
public string GetValue(string key)
{
return Regex.Replace((jObject.SelectToken(key).ToString()), @"\s", "");
} }
读取不同格式的键的内容的方法:
try
{
JObject myjobj;
JsonConfigHelper helper = new JsonConfigHelper(filepath);
myjobj = helper.jObject as JObject;//获取Jobject对象 int i = myjobj.Count; //当前对象的节点的数量
string str5 = (string)myjobj["sex"];//直接读取当前键值
MessageBox.Show(str5); JObject myjobj666;
myjobj666 = myjobj["Colleague"]["财务部"] as JObject;//当前节点下的子节点作为jobject对象
//实际测试过程中会异常报错未实例化,这是因为配件文件的保存格式问题,UTF-8
string str3 = (string)myjobj666["account"];
MessageBox.Show(str3); string str1 = helper["SOCLASS[0].Name"];//
MessageBox.Show(str1); JToken jken = myjobj["SOCLASS"];//键对象["SO"]内存在多组数据,读取的格式 foreach (JObject myobject in jken)
{
string strr = (string)myobject["Name"];//读取每组节点下的某个键值的数据
MessageBox.Show(strr);
}
if (myjobj.ContainsKey("SOCLASS")) //判断jobject对象中是否存在"SO"这个键对象
{
MessageBox.Show("done");
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
C#利用newtonsoft.json读取.so配置文件内容的更多相关文章
- 第三节:框架前期准备篇之利用Newtonsoft.Json改造MVC默认的JsonResult
一. 背景 在MVC框架中,我们可能经常会用到 return Json(),而Json方法内部又是一个JsonResult类,那么JsonResult内部又是什么原理呢?在MVC框架中,各种xxxRe ...
- Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!
using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft ...
- 利用Newtonsoft.Json实现Json序列化与反序列化
在项目中用到了Newtonsoft.Json来实现序列化和反序列化,在这里写下实现代码. 1.创建类用于排除不序列化的属性 public class ExcludePropertiesContract ...
- C# 利用Newtonsoft.Json进行Json序列化与反序列化
我们可以通过DataContractJsonSerializer类来序列化一个对象为json字符串. public class JsonConvert<T> { public static ...
- C# 利用Newtonsoft.Json 序列化生成Json数据
现在需要将一些数据转化成json格式返回给调用者, 使用Newtonsoft.Json.DLL库来帮助我们序列化 举例: {"300033":{"MC":&qu ...
- java web filter读取classpath配置文件内容
以下demo,从类路径classpath中获取venus.properties(本项目中用到的文件),思路是在初始化的时候读取,然后放在局部变量里面. package club.codeapes.we ...
- C# Newtonsoft.Json 读取文件,返回json字符串
第一种方法: public object getData2() { string content; using (StreamReader sr = new StreamReader(Server.M ...
- 利用Properties来读取JDBC配置文件
看到这样的标题,肯定会有人问,要连接数据库我直接验证,加载驱动,建立连接等等按步骤来不就行了,
- CSharp读取json配置文件内容
步骤 读取配置文件转换成字符串,代码如下 string contents = System.IO.File.ReadAllText("config.json"); 注意:该语句会抛 ...
随机推荐
- idea把菜单栏给点没了...(File、Edit、View、Navigate.......)
第一种方法 找到idea的C盘的配置文件ui.lnf.xml文件 第二种方法 如果是高版本idea,我的是2019.3,双击shift选择Actions,搜索menu 然后重启idea
- Nginx+Docker部署模式下 asp.net core 获取真实的客户端ip
目录 Nginx+Docker部署模式下 asp.net core 获取真实的客户端ip 场景 过程还原 结论 参考资料 Nginx+Docker部署模式下 asp.net core 获取真实的客户端 ...
- 5-剑指offer: 和为S的两个数字
题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. 代码 ...
- E04 【买衣服】Do you have this T-shirt in red?
核心句型 Do you have this T-shirt in red? 这样的T恤衫,你们有红色的吗? 场景对话: A:Excuse me,do you have this T-shirt in ...
- echars 实现多曲线,多Y轴,曲线单选切换,并且跟随切换指定Y轴
html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- python 并行处理数据
来源:https://blog.csdn.net/weixin_42001089/article/details/88843152 import multiprocessing import time ...
- flutter:Could not download kotlin-gradle-plugin.jar
我们可以看到是flutter的外部依赖:kotlin-gradle-plugin-1.2.71 下载不了,下面我介绍使用手动下载的方式去解决: 首先,看到flutter项目目录,注意我标出的是运行项目 ...
- mission3--dp
A---母牛的故事 题目大意:第一年有一头母牛,每年年初母牛生小母牛,小母牛第四个年头可以开始生小牛. 问第n年有多少头牛. 题解: (1)列出前几项来找规律(2)第i年牛的数量=第i-1年牛的数量+ ...
- [LeetCode] 907. Sum of Subarray Minimums 子数组最小值之和
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...