0

如何从 iDictionary 中获取好友列表(如下)?

例子

{
  "data": [
    {
      "name": "John Smith", 
      "id": "111"
    }, 
    {
      "name": "Alice Smith", 
      "id": "222"
    }, 
    {
      "name": "Mary Smith", 
      "id": "333"
    }
  ], 
  "paging": {
    "next": "https://graph.facebook.com/me/friends?format=json&limit=5000&offset=5000&__after_id=100003243976011"
  }
}
4

1 回答 1

1
  1. 您必须在应用程序中添加 System.web 和 System.Web.Extensions 程序集引用
  2. 然后尝试使用以下代码

string jsonData = @"{ ""data"": [ { ""name"": ""John Smith"", ""id"": ""111"" },
{ ""name"": ""Alice史密斯"", ""id"": ""222"" },
{ ""name"": ""Mary Smith"", ""id"": ""333"" } ],
""paging"" : { ""下一个"": ""https://graph.facebook.com/me/friends?format=json&limit=5000&offset=5000&__after_id=100003243976011"" } }";

            JavaScriptSerializer seri = new JavaScriptSerializer();
            var items = seri.Deserialize<Dictionary<string, object>>(jsonData);
        // As data in JSON is array get it deserialize as ArrayList of Dictionary<string,object>
            var dataArray =  items["data"] as ArrayList;    
        // Each item in array list contain key value pair of name and id
            foreach (Dictionary<string,object> item in dataArray)
                {
        //Read Item
                foreach (KeyValuePair<string, object> detailItem in item)
                    {
                    Console.WriteLine(detailItem.Key + " - " + detailItem.Value);
                    }
                Console.WriteLine("-------------------------------------------");
        // Read Item
                }
于 2011-12-21T04:25:05.730 回答