Newtonsoft.Json反序列化对象无法获取所有生成的键中的值

juud5qan  于 2023-01-14  发布在  其他
关注(0)|答案(2)|浏览(129)

在web端,它显示了我添加了多少文件和文件夹,但是在c#端,我无法获得这些文件和文件夹。

{
    "name": "updater",
    "type": "folder",
    "path": "updater",
    "items": [
        {
            "name": "tas",
            "type": "folder",
            "path": "updater/tas",
            "items": [
                {
                    "name": "sdsadsd.txt",
                    "type": "file",
                    "path": "updater/tas/sdsadsd.txt",
                    "byte": 6
                },
                {
                    "name": "tass",
                    "type": "folder",
                    "path": "updater/tas/tass",
                    "items": []
                },
                {
                    "name": "trexs.txt",
                    "type": "file",
                    "path": "updater/tas/trexs.txt",
                    "byte": 14
                }
            ]
        },
        {
            "name": "tas2",
            "type": "folder",
            "path": "updater/tas2",
            "items": []
        },
        {
            "name": "tas3",
            "type": "folder",
            "path": "updater/tas3",
            "items": []
        },
        {
            "name": "tas4",
            "type": "folder",
            "path": "updater/tas4",
            "items": []
        }
    ]
}

C#侧

public class jsonFF
        {
            public string name { get; set; }
            public string type { get; set; }
            public string path { get; set; }
            public jsonFF[] items { get; set; }
            public long byt { get; set; }
        }

        private void start_Click(object sender, EventArgs e)
        {
            try
            {
                WebClient updt = new WebClient();
                var updtJsonDownload = updt.DownloadString("http://localhost/update/updater.php");
                jsonFF[] json = JsonConvert.DeserializeObject<jsonFF[]>(updtJsonDownload.ToString());

                foreach (var item in json)
                {
                    string sss = item.name;
                    log.Text += sss + Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                log.Text = ex.ToString();
            }
        }

我想做的是我想做一个工具,它能自动把文件和文件夹当作json,检查它们是否在c#端的计算机上,或者应该下载并更新它们。

unguejic

unguejic1#

你的等价类在这里是错误的。我已经使用tool来得到等价类。Item类应该有byt属性同样Root类应该有items作为列表(在你的例子中你有它的类型的对象)

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Item
{
    public string name { get; set; }
    public string type { get; set; }
    public string path { get; set; }
    public List<Item> items { get; set; }
    public int? byt { get; set; }
}

public class Root
{
    public string name { get; set; }
    public string type { get; set; }
    public string path { get; set; }
    public List<Item> items { get; set; }
}
58wvjzkj

58wvjzkj2#

您必须修复jsonFolder类。将“对象项“替换为“列表< jsonFolder>项“(或“jsonFolder[]项”)

jsonFolder[] items = JsonConvert
               .DeserializeObject<jsonFolder>(updtJsonDownload.ToString()).items;

    foreach (var item in items)
    {
      string sss = item.name;
      log.Text += sss + Environment.NewLine;
     }

 //or shorter
log.Text=string.Join("\n", items.Select(i =>i.name ));

//if you need file names
log.Text = string.Join("\n", items.SelectMany(i =>i.items.Select(it =>it.name ) ))); 

//if you need  pathes
string[] pathes = items.SelectMany(i =>i.items.Select(it =>it.path)).ToArray();

public class jsonFolder
{
    public string name { get; set; }
    public string type { get; set; }
    public string path { get; set; }
    public List<jsonFolder> items { get; set; }
    public int? byt { get; set; }
}

相关问题