我有这种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/