3番目のループに到達すると、テーブル上に「行」が見つからないために爆発します。どうして?Newtonsoft.JSONで動的JSONを解析すると、逆シリアル化されたオブジェクトに配列がありません
var converter = new ExpandoObjectConverter();
dynamic deserializeObject = JsonConvert.DeserializeObject<ExpandoObject>(jsonString, converter);
foreach (var model in deserializeObject.Model)
{
foreach (var table in model.Table)
{
foreach (var row in table.Row)
{
Console.WriteLine(row.BookId + ": " + row.BookName);
}
}
}
JSON:
{
"Model": [
{
"Field1": "Field1Value",
"Field2": "Field2Value",
"Field3": "Field3Value",
"Table": {
"Row": [
{
"BookId": "1",
"BookName": "Computer Architecture",
"Category": "Computers",
"Price": "125.60"
},
{
"BookId": "2",
"BookName": "Asp.Net 4 Blue Book",
"Category": "Programming",
"Price": "56.00"
},
{
"BookId": "3",
"BookName": "Popular Science",
"Category": "Science",
"Price": "210.40"
},
{
"BookId": "4",
"BookName": "Mission Impossible",
"Category": "Adventure",
"Price": "210.40"
}
]
}
},
{
"ClientFirstName": "Jane",
"ClientLastName": "Doe",
"Table": [
{
"Row": [
{
"BookId": "1",
"BookName": "Computer Architecture",
"Category": "Computers",
"Price": "125.60"
},
{
"BookId": "3",
"BookName": "Popular Science",
"Category": "Science",
"Price": "210.40"
},
{
"BookId": "4",
"BookName": "Mission Impossible",
"Category": "Adventure",
"Price": "210.40"
}
]
},
{
"Row": [
{
"BookId": "1",
"BookName": "Computer Architecture",
"Category": "Computers",
"Price": "125.60"
},
{
"BookId": "4",
"BookName": "Mission Impossible",
"Category": "Adventure",
"Price": "210.40"
}
]
}
]
}
]
}
http://jsonlint.com/ – tofutim
であなたのJSONをチェックしますコードの再フォーマット – Thomas