2012-11-03 35 views
0

這不是工作(該文件存在,它讀取就OK了,那麼沒有環火),我不知道爲什麼..C#JSON讀不會發生

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" 
    } 
] 
+0

這是確定它是簡單://公共ArrayList的文件夾//在jsonRead - 我會假設JSON會腐爛,並通過它作爲一個字符串 - 但很明顯不是; – conners

回答

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