关于Newtonsoft.Json,LINQ to JSON的一个小demo
nuget获取Newtonsoft.Json
github地址:Newtonsoft.Json
public static void Test1()
{
/* 文本格式如下 代码实现目的:
1、VR 对象中是否包含ProjectId
2、取ProjectId下的值
{
"VR": {
"ProjectId": {
"txt_s": "工程项目的名称编号Id",
"value": "01"
},
"ProjectObject": {
"ProjectTypeName": {
"name": "12企业类别1",
"txt_s": "企业类别名称"
},
"txt_s": "企业类别对象"
},
"ProjectTitle": {
"txt_s": "工程项目的名称(Title信息)",
"value": "01"
},
"UserId": {
"txt_s": "用户的Id",
"value": "01"
}
}
}
*/
var str = @"%7b+%22VR%22%3a+%7b%22ProjectId%22%3a+%7b%22txt_s%22%3a+%22%e5%b7%a5%e7%a8%8b%e9%a1%b9%e7%9b%ae%e7%9a%84%e5%90%8d%e7%a7%b0%e7%bc%96%e5%8f%b7Id%22%2c+%22value%22%3a+%2201%22+%7d%2c+%22ProjectObject%22%3a+%7b%22ProjectTypeName%22%3a+%7b%22name%22%3a+%2212%e4%bc%81%e4%b8%9a%e7%b1%bb%e5%88%ab1%22%2c+%22txt_s%22%3a+%22%e4%bc%81%e4%b8%9a%e7%b1%bb%e5%88%ab%e5%90%8d%e7%a7%b0%22+%7d%2c+%22txt_s%22%3a+%22%e4%bc%81%e4%b8%9a%e7%b1%bb%e5%88%ab%e5%af%b9%e8%b1%a1%22+%7d%2c+%22ProjectTitle%22%3a+%7b%22txt_s%22%3a+%22%e5%b7%a5%e7%a8%8b%e9%a1%b9%e7%9b%ae%e7%9a%84%e5%90%8d%e7%a7%b0(Title%e4%bf%a1%e6%81%af)%22%2c+%22value%22%3a+%2201%22+%7d%2c+%22UserId%22%3a+%7b%22txt_s%22%3a+%22%e7%94%a8%e6%88%b7%e7%9a%84Id%22%2c+%22value%22%3a+%2201%22+%7d+%7d%7d";
//上述文本是编码之后的,方便调试
str = HttpUtility.UrlDecode(str, Encoding.UTF8);
var jobject = JObject.Parse(str);
var projectId = jobject.SelectToken("VR.ProjectId");
//VR 对象中是否包含ProjectId
if (!projectId.HasValues)
{
//不包含
}
else
{
foreach (var temp in projectId.Children())
{
if (temp.Path.EndsWith("txt_s"))
{
var a = temp.ToObject<string>();
//a 的值是 工程项目的名称编号Id
}
if (temp.Path.EndsWith("value"))
{
var a = temp.ToObject<string>();
//a 的值是 01
}
}
}
}