循环列表和JToken JSON的子列表

时间:2016-11-17 09:57:57

标签: c# json

我有一个JSON文件和列表,其子列表如下:

ListCmsControl 
|
|----ControlList
|        Name
|          |----ControlList
|                 Name
|          |----ControlList
|                 Name   
|
|----ControlList
|          |----ControlList
|          |----ControlList
|
|----ControlList
|          |----ControlList
|                   Name   
|          |----ControlList
|                   Name

和CmsControls CLASS:

public class CmsControls
{
    public string Name { get; set; }
    public List<CmsControls> ControlList { get; set; }
}

我想在此列表中循环并获取每个列表控件的名称

我试过这段代码:

public List<CmsControls> GetCmsControls()
{
    List<CmsControls> controls = new List<CmsControls>();

    foreach (var CmsControls in BaseToken.SelectToken("ListCmsControl"))
    {

        cmsControls(CmsControls, ref controls);
    }

    return controls;
}

private void cmsControls(JToken controls, ref List<CmsControls> ctrl)
{
    foreach (var control in controls)
    {
        var jtoken = control.SelectToken("ControlList");
        ctrl.Add(jtoken.ToObject<CmsControls>());
        cmsControls(jtoken, ref ctrl);
    }
}

但我只获得了第一级。

0 个答案:

没有答案