我目前正試圖從json文件中另一個數組內的數組中獲取數據。可惜我不是在LINQ真正懂行的,所以我做的這一切在C#JSON文件的使用JSON.Net解析JArray內部的JArray
樣品
"Fruits": [
{
"type": "banana",
"FunFact": [
{
"$": "Yellow"
},
{
"$": "Long"
},
{
"$": "People love them"
}
]
}
]
目前,這是我的代碼。
var csfdata = token.Value<JArray>("Fruit");
//There are 8 of these Fruit Arrays
var csfcount = csfdata.Count;
Console.WriteLine("Length of CSFArray is {0}", csfcount.ToString());
for (int i = 0; i < csfcount; i++)
{
//Work just fine
Console.WriteLine("Fruit: {0}", token.SelectToken("Fruit[" + i.ToString() +"].type").ToString());
//Crashes here saying that FunFact is null or empty when I am trying to get a count of how many is in FunFact
var tfdata = token.Value<JArray>("FunFact");
var tfcount = tfdata.Count;
}
任何建議,不幸的是,我不能在此刻
var tfdata = csfdata [i] .Value(「FunFact」);是正確的答案。非常感謝!如果我想知道它是一個數組還是一個對象,後續bu的道歉,我怎麼能通過代碼來告訴。如果我這樣做: if(csfdata [i] .Value (「FactorText」)is JArray) 我得到一個從JObject到JArray的無效轉換 –
JuniorFlip