您的位置:首页 - 教程 - C# - 正文
C#解析Json

 解析:{'id':'4028d80858053bed0158053ef7a50001','sl':0.0,'sfyfz':'0','zwjyzsbh':'1000001600000018'}

 1、新建winform控制台项目

2、在项目里新建一个实体类test.cs

class test
    {
       
        public string id { get; set; }
        public string sl { get; set; }
        public string sfyfz { get; set; }
        public string zwjyzsbh { get; set; }
        public string shlb { get; set; }
        public string zsh { get; set; }
       
    }

 3、在Program.cs的Main方法里(方法一)

class Program
    {
        static void Main(string[] args)
        {
           
            string json=@"[{'id':'22222222','sl':0.0,'sfyfz':'0','zwjyzsbh':'333333333'}]";
            
            List<test> jobInfoList = JsonConvert.DeserializeObject<List<test>>(json);
            foreach (test jobInfo in jobInfoList)
            {
              
                Console.WriteLine("id:" + jobInfo.id);
            }
            Console.ReadLine();
        }
    }

要引用Newtonsoft.Json(nuget下载)

 

 

 

3、在Program.cs的Main方法里(方法二)

class Program
    {
        static void Main(string[] args)
        {
           
            string json = @"[{'id':'4028d80858053bed0158053ef7a50001','sl':0.0,'sfyfz':'0','zwjyzsbh':'1000001600000018'}]";
            JsonReader reader = new JsonTextReader(new StringReader(json));

            while (reader.Read())
            {
                Console.WriteLine(reader.Value);
            }
            Console.ReadLine();
        }
    }

评论: