我们如何在C#中的JSON数据中遍历子节点

时间:2018-11-25 10:16:02

标签: c#

我编写了一个c#代码,以通过HTTPREQUEST提取JSON数据。 json数据在父节点内有多个子节点。 在下面,我附加了一个示例JSON数据格式以及我构建为遍历节点的代码。我的代码仅检查子节点的第1级(子节点内将有子节点)。

  1. 任何人都可以帮助我更改代码以遍历多个子节点。 2.此外,我想在每个网络请求后应用1分钟的延迟。

    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://url.com/api/section);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "GET";
    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));
        try
        {
            using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
            {
                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream);
                str4 = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
            }
            foreach (forumsec.forumsec forumsec in JsonConvert.DeserializeObject<List<forumsec.forumsec>>(str4))
            {
                long id = forumsec.id;
                TableOperation operation = TableOperation.InsertOrMerge((ITableEntity)new forumsecEntity(id.ToString())
                {
                    pid= forumsec.pid,
                    name = forumsec.name,
                 });
     HttpWebRequest httpWebRequest1 = (HttpWebRequest)WebRequest.Create("https://https://url.com/api/section?parentid=" + id.ToString());
        httpWebRequest1.ContentType = "application/json";
        httpWebRequest1.Method = "GET";
    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));
                using (HttpWebResponse response = (HttpWebResponse)httpWebRequest1.GetResponse())
                    {
                        Stream responseStream = response.GetResponseStream();
                        StreamReader streamReader = new StreamReader(responseStream);
                        str4 = streamReader.ReadToEnd();
                        streamReader.Close();
                        responseStream.Close();
                foreach (Childcategories forum in JsonConvert.DeserializeObject<List<Childcategories>>(str4))
                    {
                        long caid = forum.categoryid;
                        TableOperation operation1 = TableOperation.InsertOrMerge((ITableEntity)new ChildcategoriesEntity(caid.ToString())
                        {
                            pid = forum.pid,
                            name = forum.name
    
                        }
                    }                   
                    }
            }
    

JSON文件格式

{
ID: 123
Name: BACD
ParentID: 
Children: [
        ID: 765
        Name: atdr
        ParentID: 123 
        Children: [ 
                    ID: 098
                    Name: jryr
                    ParentID: 765
                    Children: []
                    ]
        ]

0 个答案:

没有答案