我正在查詢API並將數據返回給C#控制檯應用程序。我能夠成功查詢我的數據,除了這樣的數組元素。這是該數據在JSON格式用C解析JSON數組#
[
{
"UserType": "Admin",
"UserPerms:" [
{
"level1:" "Yes",
"level2:" "No",
"level3:" "No",
"level4:" "Yes",
"level5:" [
{
"door1:" "Yes",
"door2:" "No",
"door3:" "No",
"doory4:" "Yes"
}
]
}
]
}
]
回來,這是我嘗試使用返回數據的C#語法 - 什麼是正確的C#語法來回報每門值LEVEL5?
public class RootObject
{
public string UserType { get; set; }
public List<UserPerms> UserPerms { get; set; }
}
public class UserPerms
{
public string level1 { get; set; }
public string level2 { get; set; }
public string level3 { get; set; }
public string level4 { get; set; }
public string[] level5 { get; set; }
}
public class Main[]
{
var response = syncClient.DownloadString(url);
var o = JsonConvert.DeserializeObject<RootObject[]>(response);
foreach (RootObject ro in o)
if (ro.UserPerms != null)
foreach (UserPerms info in ro.UserPerms)
{
Console.WriteLine("Access to Level1" + info.level1);
Console.WriteLine("Access to Level2" + info.level2);
Console.WriteLine("Access to Level3" + info.level3);
Console.WriteLine("Access to Level4" + info.level4);
Console.WriteLine("Access to Level5" + info.level5);
}
}
這個模式的意圖是什麼,你把所有的東西都放在一個數組中,其中有一個對象呢? –
@EdPlunkett - for level5?或者整體語法?如果總體語法 - 這是從某人繼承而來的,並且語法有效,所以我去了它... – user2676140
這是一個奇怪的結構,似乎不必要地使事情複雜化。 – Amy