我有這樣的JSON結構:在我的C#示例解釋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);
,但我只得到空的列表。你能告訴我問題在哪裏嗎?
你也可以嘗試其他方法:創建一個Card,將它序列化爲JSON並查看輸出結果是什麼? – Rup
你爲什麼使用動態? - 結果是已知類型(文章),因此您可以編寫文章或變種。 –
您的文章類的定義在上面丟失。 –