0

This isn't working (the file exists, it reads it ok, then none of the loops fire) and I don't know why..

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);                    
        }
    }
}

the classes

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;
}

the json

[   
  {
    "Folders": [
      {
        "localFolder": "C:\\test",
        "serverFolder": "Default/"
      }
    ],
    "Name": "hi",
    "User": "there",
    "Password": "secret",
    "Url": "https://mydomain.com"
  } 
]
1
  • it's ok it was simply this: //public ArrayList Folders// in jsonRead - I would have assumes that the json would have decayed and passed it as a string - but obviously not; Commented Nov 3, 2012 at 14:02

1 Answer 1

1

You should define jsonRead like below:

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

And your code:

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.