将 IDictionary 与 Json 一起使用?

发布于 2024-12-22 03:20:45 字数 798 浏览 0 评论 0原文

如何将朋友列表(如下)放入 iDictionary 中或从 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"
  }
}

how to get the friends list (below) into and out from a iDictionary?

Example

{
  "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"
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生殊途 2024-12-29 03:20:45
  1. 您必须在应用程序中添加 System.web 和 System.Web.Extensions 程序集引用
  2. 然后尝试使用以下代码

string jsonData = @"{ ""data"": [ { ""name"": ""John Smith"", ""id"": ""111"" },
{ ""name"": ""Alice Smith"", ""id"": ""222"" },
{ ""name"": ""玛丽·史密斯"", ""id"": ""333"" } ],
""分页"": { ""下一个"": ""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
                }
  1. You have to add System.web and System.Web.Extensions assembly reference in your application
  2. Then try to use following code

string jsonData = @"{ ""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"" } }";

            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
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文