欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中解析JSON数据,并获取到其中的值

程序员文章站 2022-04-08 17:53:29
1.应需求创建一个Json字符串 string json = "[{'Name':'张三','age':'20','Data':{'ID':100,'heigh':'180','weight':'90'}}]"; 2.根据字符串的数据结构定义两个类: public class User { publ ......

1.应需求创建一个json字符串

  string json = "[{'name':'张三','age':'20','data':{'id':100,'heigh':'180','weight':'90'}}]";

2.根据字符串的数据结构定义两个类:

  public class user
        {
            public string name{ get; set; }
            public string age{ get; set; }
            public data data{ get; set; }
        }

  public class data
        {
            public int id{ get; set; }
            public string heigh{ get; set; }
            public string weight{ get; set; }
        }

3.再main函数中进行操作,如下main函数:

  static void main(string[] args)
        {
          string json = "[{'name':'张三','age':'20','data':{'id':100,'heigh':'180','weight':'90'}}]";
            list<user> jobinfolist = newtonsoft.json.jsonconvert.deserializeobject<list<user>>(json);

            foreach (info jobinfo in jobinfolist)
            {
                console.writeline("username:" + jobinfo.name);
                console.writeline("username:" + jobinfo.data.id);
            }
        }

  public class user
        {
            public string name{ get; set; }
            public string age{ get; set; }
            public data data{ get; set; }
        }

  public class data
        {
            public int id{ get; set; }
            public string heigh{ get; set; }
            public string weight{ get; set; }
        }