我有这种json结构:
{
"Root": {
"data": [
{
"CardName": "card1",
"functions": [
{
"State": "OPEN",
"State": "INHERENT"
}
]
},
{
"CardName": "card2",
"functions": [
{
"State": "CLOSED",
"State": "INHERENT"
}
]
}
]
}
}
我的 C# 类是:
[DataContract]
public class Card
{
[DataMember(Name = "CardName")]
public string CardName { get; set; }
[DataMember(Name = "functions")]
public List<Function> Functions { get; set; }
}
[DataContract]
public class Function
{
[DataMember(Name = "State")]
public string State { get; set; }
}
我想解析该结构以获得卡片列表,并且每张卡片都包含一个函数列表。
此刻我正在尝试这个:
string content = string.Empty;
using (StreamReader sr = new StreamReader("json"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
content += line;
}
}
List<Card> dynObj = JsonConvert.DeserializeObject<Card>(content);
但我只得到一个空值列表。你能告诉我问题出在哪里吗?
最佳答案
通过在 Visual Studio 中粘贴 JSON(编辑 > 选择性粘贴 -> 将 JSON 作为类粘贴),它告诉我你的数据的类应该如下所示。
public class Rootobject
{
public Root Root { get; set; }
}
public class Root
{
public Datum[] data { get; set; }
}
public class Datum
{
public string CardName { get; set; }
public Function[] functions { get; set; }
}
public class Function
{
public string State { get; set; }
}
关于c# - 在我的 C# 示例中解释 JSON 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19244924/