0
我正在開發VS 2012中的Windows 8商店應用程序,並且新增了C#和XAML。使用JsonArray解析Json訂閱源如何從另一個數組中的數組中獲取元素?C#解析JSON數組元素
[
{
"item_id": 2270978,
"item_title": "This is the title",
"roles": [
{
"item_url": "http://www.blahblah.com/161089",
"parent_url": "http://www.blahblah.com",
},
{
"item_url": "http://www.blahblah.com/469888",
"parent_url": "http://www.blahblah.com",
}
],
}
]
我想另一個的foreach將原來的foreach內工作,但我得到一個錯誤:
Cannot apply indexing with [] to an expression of type 'Windows.Data.Json.IJsonValue'
foreach (var item in items)
{
JsonObject theItem = item.GetObject();
FeedItem feedItem = new FeedItem();
feedItem.PubDate = System.DateTime.Now;
feedItem.Title = theItem["item_title"].GetString();
feedItem.Author = "Me";
feedItem.ItemId = (int)(theItem["item_id"].GetNumber());
feedItem.Content = theItem["item_body"].GetString();
feedItem.Link = new Uri(theItem["permalink_url"].GetString());
var roles = theItem["roles"].GetArray();
/* TRIED LIKE THIS DOESN'T WORK
foreach (var role in roles)
{
feedItem.ContentUrl = new Uri(role["item_url"].GetString());
}
*/
feed.Items.Add(feedItem);
}
是的,它是item_url,我剛剛改變它。 –
我已經更新了我的答案。現在檢查。 – MarcinJuraszek
我改變了變量,我會再次更改它們以匹配我的原始代碼。非常感謝,你搖滾! –