一、使用命名空间
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
二、文件打开
System.IO.File.Exists(JsonFileStr)
//读取json文件
using(System.IO.StreamReader jsfile = System.IO.File.OpenText(JsonFileStr))
//转成jsontext
using(JsonTextReader rd = new JsonTextReader(jsfile))
//转成jobject
JObject jo = (JObject)JToken.ReadFrom(rd);
三、判断key
{
“JPEG”: {},
“JFIF”: {
“Version”: “1.1”,
“Resolution Units”: “inch”,
“X Resolution”: “72 dots”,
“Y Resolution”: “72 dots”,
“Thumbnail Width Pixels”: “0”,
“Thumbnail Height Pixels”: “0”
},
}
3.1判断key中的jpeg存不存在
string y1 = “jpeg“;
string y2 = “Version“;
if (jo.Property(y1) != null)//判断对应的键值存不存在,如果不存在,则退出
3.2判断属性Version存不存在
if (jo[y1][y2] != null)
四、容易出现的错误
//if (jo.ToString().Contains(y1))//判断键值存不存在
//if (jo[y1].ToString().Contains(y2))//判断属性是否包含
原创文章,作者:寂寞沧桑了谁的博客,如若转载,请注明出处:https://www.zengqueling.com/c%e4%b8%8b%e7%9a%84json%e8%af%bb%e5%8f%96key%e5%92%8c%e5%b1%9e%e6%80%a7%e7%9a%84%e5%88%a4%e6%96%ad/