0

这不起作用(文件存在,读取正常,然后没有任何循环触发)而且我不知道为什么..

if (File.Exists("my.json"))
{
    var response = System.IO.File.ReadAllText("my.json");
    var domains = JsonConvert.DeserializeObject<List<jsonRead>>(response);
    foreach (jsonRead domain in domains)
    {
        MessageBox.Show(domain.Password);
        var folders = JsonConvert.DeserializeObject<List<jsonReadDeep>>(domain.Folders);

        foreach (jsonReadDeep folder in folders)
        {
            MessageBox.Show(folder.localServer);                    
        }
    }
}

班级

class jsonRead
{
    public string Folders;
    public string Name;
    public string User;
    public string Password;
    public string Url;
}

class jsonReadDeep
{
    public string localFolder;
    public string serverFolder;
}

json

[   
  {
    "Folders": [
      {
        "localFolder": "C:\\test",
        "serverFolder": "Default/"
      }
    ],
    "Name": "hi",
    "User": "there",
    "Password": "secret",
    "Url": "https://mydomain.com"
  } 
]
4

1 回答 1

1

您应该定义jsonRead如下:

class jsonRead
{
    public jsonReadDeep[] Folders;
    public string Name;
    public string User;
    public string Password;
    public string Url;
}

你的代码:

var response = System.IO.File.ReadAllText("my.json");
var domains = JsonConvert.DeserializeObject<List<jsonRead>>(response);

foreach (jsonRead domain in domains)
{
    MessageBox.Show(domain.Password);

    foreach (jsonReadDeep folder in domain.Folders)
    {
        MessageBox.Show(folder.localFolder);
    }
}
于 2012-11-03T14:10:58.517 回答