Json中判断键值为空

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<JProperty> properties = jo.Properties();
            foreach (JProperty item in properties)
            {
                Console.WriteLine(item.Name + ":" + item.Value);
            }
            Console.ReadKey();
        }
    }
}
参考:
http://q.cnblogs.com/q/46146/

猜你喜欢

转载自blog.csdn.net/zcg1041/article/details/79972358