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

C#获取 json嵌套的数组

程序员文章站 2022-06-12 17:06:14
...

文章目录


C#获取 json嵌套的数组

json对象datas:

{"type":1,"message":null,"resultdata":{"totalCount":2,"pageSize":30,"pageNo":1,"pageCount":
1,"list":[{"meterNo":"000041100039","date":"20190308114451687","degree":456.0},{"meterNo":"000041100039","date":"20190308113517787","degree":426.0}]}}

我们的目的是要获取list中的两组数据。首先声明一个list类用于方便接收数据

List<getDegreelist> degreelists = new List<getDegreelist>();
public class getDegreelist
        {
            public string meterNo { get; set; }
            public string date { get; set; }
            public string degree { get; set; }
        }

通过JArray对象将获取datas中的list

JArray array = JArray.Parse(datas["list"].ToString());

循环添加到degreelists里面

for (int i = 0; i < array.Count; i++)
            {
                getDegreelist list = new getDegreelist();
                JObject jb = JObject.Parse(array[i].ToString());
                list.meterNo = jb["meterNo"].ToString();
                list.date = jb["date"].ToString();
                list.degree = jb["degree"].ToString();
                degreelists.Add(list);
            }