C# NEWTONSOFT.JSON读取json文件
Posted 分享使用快乐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# NEWTONSOFT.JSON读取json文件相关的知识,希望对你有一定的参考价值。
public static void Readjson()
{
string jsonfile = "D://tsconfig1.json";
using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject o = (JObject)JToken.ReadFrom(reader);
string a = o["lotname"].ToString();
var b = o["other"];
var c = b["lotaddress"];
var d = o["devices"];
foreach(JObject e in d)
{
var deviceID = e["deviceID"];
var name = e["name"];
var IP = e["IP"];
}
}
}
}
tsconfig1.json文件内容
{
"lotname": "停车系统",
"devices": [
{
"deviceID": "EI1001",
"name": "东道进口相机",
"type": "进口",
"IP": "192.168.1.100"
},
{
"deviceID": "EI1002",
"name": "东道进口语音屏",
"type": "进口",
"IP": "192.168.1.102"
},
{
"deviceID": "EO1003",
"name": "东道出口相机",
"type": "出口",
"IP": "192.168.1.103"
},
{
"deviceID": "EO1004",
"name": "东道出口语音屏",
"type": "出口",
"IP": "192.168.1.104"
}
],
"other": { "lotname": "wz001", "lotaddress": "wenzhou" }
}
注意:记事本另存为 以上内容时编码选择 U-TF8
以上是关于C# NEWTONSOFT.JSON读取json文件的主要内容,如果未能解决你的问题,请参考以下文章
c# 在.NET使用Newtonsoft.Json转换,读取,写入json
C#利用newtonsoft.json读取.so配置文件内容
c# 读取json的问题,JObject不能强转成JArray
读取 webapi 2 令牌时读取 HttpResponseMessage.Content 会引发 Newtonsoft.Json.JsonReaderException