来源: C# json解析时,得到JObject后怎么判断它的某一键值是否存在?_博问_博客园
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO;
namespace JsonTest
{
class Program
{
static void Main(string[] args)
{
string str = “{\”3\”:123,body:456,list:{title:’abc’,body:’what’}}”;
JObject jo = JObject.Parse(str);
if (jo.Property(“3”) == null || jo.Property(“3”).ToString() == “”)
{
Console.WriteLine(“键值key不存在!”);
}
bool hasErr = jo.Properties().Any(p => p.Name == “err”)//或是这样
IEnumerable properties = jo.Properties();
foreach (JProperty item in properties)
{
Console.WriteLine(item.Name + “:” + item.Value);
}
Console.ReadKey();
}
}
}
参考:
http://q.cnblogs.com/q/46146/