【问题标题】:Deserialization of a Facebook JSON string?Facebook JSON字符串的反序列化?
【发布时间】:2012-02-18 14:14:52
【问题描述】:

我无法从运行 fql 查询后生成的 JSON 字符串中提取 facebook 用户的 work_history、隶属关系和 current_loc

我已经做了这门课:

public class Data
{
    public CurrentLocation current_location { get; set; }
    public WorkHistory[] work_history { get; set; }
    public EducationHistory[] education_history { get; set; }
}

public class EducationHistory
{
    public string name { get; set; }
    public int? year { get; set; }
    public string school_type { get; set; }
}

public class WorkHistory
{
    public string company_name { get; set; }
    public Location location { get; set; }
}

public class CurrentLocation
{
    public string city { get; set; }
    public string state { get; set; }
    public string country { get; set; }
}

public class Location
{
    public string city { get; set; }
    public string state { get; set; }
}

我的 JSON 字符串的格式是:

   {"current_location":{"city":"Delhi","state":"Delhi","country":"India","zip":"","id":102161913158207,"name":"Delhi, India"}

,"education_history":[{"name":"I E T , Alwar (Raj.)","year":2007,"concentrations":[],"school_type":"College"},{"name":"Institute of Engineering and Technology, Alwar","concentrations":[],"school_type":"College"}],

"work_history": [{"location":{"city":"","state":""},"company_name":"Defence Research & Development Organisation (DRDO)","description":"","start_date":"0000-00","end_date":"0000-00"}]}

我如何提取这些数据当我使用这个代码时我得到一个空值:

Data soap = JsonConvert.DeserializeObject<Data>(ser);

编辑:我确实根据答案建议更改了代码,但我仍然没有得到任何值

编辑 2: 如果我只有一个用户,所提供的解决方案会起作用,但是我有几个用户拥有这些详细信息,我如何获取这些数据?下面为不同的用户提供了几个类似的值

   {
    "data": [
        {
            "uid": 54456565,
            "name": "dgfg Barma",
            "pic_square": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash2/275277_513046358_147089990_q.jpg",
            "affiliations": [
                {
                    "nid": 33605704,
                    "name": "Our Lady Queen of the Missions School",
                    "type": "high school"
                }
            ],
            "birthday_date": null,
            "sex": "female",
            "relationship_status": null,
            "current_location": {
                "city": "Delhi",
                "state": "Delhi",
                "country": "India",
                "zip": "",
                "id": 102161913158207,
                "name": "Delhi, India"
            },
            "education_history": [
                {
                    "name": "Amity University",
                    "concentrations": [],
                    "school_type": "College"
                },
                {
                    "name": "Amity School of Engineering & Technology",
                    "concentrations": [],
                    "school_type": "College"
                }
            ],
            "work_history": []
        },
        {
            "uid": 520310824,
            "name": "Mamta Meena",
            "pic_square": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/370835_520310824_553662476_q.jpg",
            "affiliations": [],
            "birthday_date": "12/03/1986",
            "sex": "female",
            "relationship_status": "Married",
            "current_location": {
                "city": "Delhi",
                "state": "Delhi",
                "country": "India",
                "zip": "",
                "id": 102161913158207,
                "name": "Delhi, India"
            },
            "education_history": [
                {
                    "name": "I E T , Alwar (Raj.)",
                    "year": 2007,
                    "concentrations": [],
                    "school_type": "College"
                },
                {
                    "name": "Institute of Engineering and Technology, Alwar",
                    "concentrations": [],
                    "school_type": "College"
                }
            ],
            "work_history": [
                {
                    "location": {
                        "city": "",
                        "state": ""
                    },
                    "company_name": "Defence Research & Development Organisation (DRDO)",
                    "description": "",
                    "start_date": "0000-00",
                    "end_date": "0000-00"
                }
            ]
        },
        {
            "uid": 6565767,
            "name": "gfgfg Agarwal",
            "pic_square": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash2/372433_529863326_1656605196_q.jpg",
            "affiliations": [
                {
                    "nid": 16827518,
                    "name": "Amity University",
                    "type": "college"
                }
            ],
            "birthday_date": "07/18",
            "sex": "female",
            "relationship_status": null,
            "current_location": {
                "city": "Delhi",
                "state": "Delhi",
                "country": "India",
                "zip": "",
                "id": 102161913158207,
                "name": "Delhi, India"
            },
            "education_history": [
                {
                    "name": "Amity University, Noida",
                    "year": 2012,
                    "concentrations": [],
                    "school_type": "College"
                },
                {
                    "name": "Amity University",
                    "concentrations": [],
                    "school_type": "Grad School"
                }
            ],
            "work_history": []
        },
        {
            "uid": 542580357,
            "name": "bnnm Gupta",
            "pic_square": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/372576_542580357_1330958366_q.jpg",
            "affiliations": [],
            "birthday_date": "04/30",
            "sex": "female",
            "relationship_status": "Single",
            "current_location": {
                "city": "New Delhi",
                "state": "Delhi",
                "country": "India",
                "zip": "",
                "id": 106517799384578,
                "name": "New Delhi, India"
            },
            "education_history": [
                {
                    "name": "Amity University",
                    "concentrations": [],
                    "school_type": "College"
                },
                {
                    "name": "Amity University, Noida",
                    "concentrations": [],
                    "school_type": "College"
                }
            ],
            "work_history": [
                {
                    "location": {
                        "city": "",
                        "state": ""
                    },
                    "company_name": "Oracle India Pvt Ltd",
                    "position": "Intern",
                    "description": "",
                    "start_date": "2011-05"
                }
            ]
        },
        {
            "uid": 544204117,
            "name": "pos Gupta",
            "pic_square": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/275346_544204117_594313449_q.jpg",
            "affiliations": [
                {
                    "nid": 16827518,
                    "name": "Amity University",
                    "type": "college"
                }
            ],
            "birthday_date": "05/17/1990",
            "sex": "female",
            "relationship_status": "Single",
            "current_location": {
                "city": "Noida",
                "state": "Uttar Pradesh",
                "country": "India",
                "zip": "",
                "id": 130646063637019,
                "name": "Noida, India"
            },
            "education_history": [
                {
                    "name": "Amity University, Noida",
                    "year": 2012,
                    "concentrations": [],
                    "school_type": "College"
                },
                {
                    "name": "Amity University",
                    "concentrations": [],
                    "school_type": "Grad School"
                }
            ],
            "work_history": []
        },
        {
            "uid": 4567786,
            "name": "orr Makhijani",
            "pic_square": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/372443_555253796_244086438_q.jpg",
            "affiliations": [
                {
                    "nid": 16827518,
                    "name": "Amity University",
                    "type": "college"
                }
            ],
            "birthday_date": null,
            "sex": "female",
            "relationship_status": null,
            "current_location": null,
            "education_history": [
                {
                    "name": "Amity University",
                    "concentrations": [],
                    "school_type": "College"
                }
            ],
            "work_history": []
        },
        {
            "uid": 556773604,
            "name": "Nazima Khalid",
            "pic_square": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash2/372542_556773604_1472126324_q.jpg",
            "affiliations": [],
            "birthday_date": "03/22",
            "sex": "female",
            "relationship_status": "Married",
            "current_location": {
                "city": "Toronto",
                "state": "Ontario",
                "country": "Canada",
                "zip": "",
                "id": 110941395597405,
                "name": "Toronto, Ontario"
            },
            "education_history": [
                {
                    "name": "AMU",
                    "concentrations": [],
                    "school_type": "College"
                }
            ],
            "work_history": [
                {
                    "location": {
                        "city": "Oakville",
                        "state": "Ontario"
                    },
                    "company_name": "SNC-Lavalin Inc.",
                    "position": "Contracts",
                    "description": "",
                    "start_date": "0000-00"
                }
            ]
        }
    ]
}

请帮忙

【问题讨论】:

    标签: c# asp.net json facebook-graph-api json.net


    【解决方案1】:

    我不会声明很多小类,而是采用dynamic 方式。这是dynamic json object 的代码。用法是这样的:

    dynamic obj = JsonUtils.JsonObject.GetDynamicJsonObject(jsonString);
    
    Console.WriteLine("{0} , {1}", obj.current_location.city, obj.current_location.state);
    
    Console.WriteLine("EDUCATION");
    foreach (var eduHist in obj.education_history)
    {
        Console.WriteLine("{0} , {1}", eduHist.name, eduHist.year);
    }
    
    Console.WriteLine("WORK");
    foreach (var workHist in obj.work_history)
    {
        Console.WriteLine("{0}  {1}-{2}", workHist.company_name, workHist.start_date, workHist.end_date);
    }
    

    【讨论】:

    • 我的代码无法读取 JsonUtils 它属于什么命名空间?
    • 你可以这样使用:动态数据 = Newtonsoft.Json.JsonConvert.DeserializeObject(json);如stackoverflow.com/questions/8738031/… 所述
    【解决方案2】:

    您需要使用与您的 JSON 结构匹配的正确属性名称:

    using System;
    using Newtonsoft.Json;
    
    public class Data
    {
        public CurrentLocation current_location { get; set; }
        public WorkHistory[] work_history { get; set; }
        public EducationHistory[] education_history { get; set; }
    }
    
    public class EducationHistory
    {
        public string name { get; set; }
        public int? year { get; set; }
        public string school_type { get; set; }
    }
    
    public class WorkHistory
    {
        public string company_name { get; set; }
        public Location location { get; set; }
    }
    
    public class CurrentLocation
    {
        public string city { get; set; }
        public string state { get; set; }
        public string country { get; set; }
    }
    
    public class Location
    {
        public string city { get; set; }
        public string state { get; set; }
    }
    
    class Program
    {
        static void Main()
        {
            var json = 
            @"
            {
                ""current_location"": {
                    ""city"": ""Delhi"",
                    ""state"": ""Delhi"",
                    ""country"": ""India"",
                    ""zip"": """",
                    ""id"": 102161913158207,
                    ""name"": ""Delhi, India""
                },
                ""education_history"": [
                    {
                        ""name"": ""I E T , Alwar (Raj.)"",
                        ""year"": 2007,
                        ""concentrations"": [],
                        ""school_type"": ""College""
                    },
                    {
                        ""name"": ""Institute of Engineering and Technology, Alwar"",
                        ""concentrations"": [],
                        ""school_type"": ""College""
                    }
                ],
                ""work_history"": [
                    {
                        ""location"": {
                            ""city"": """",
                            ""state"": """"
                        },
                        ""company_name"": ""Defence Research & Development Organisation (DRDO)"",
                        ""description"": """",
                        ""start_date"": ""0000-00"",
                        ""end_date"": ""0000-00""
                    }
                ]
            }";
    
            Data soap = JsonConvert.DeserializeObject<Data>(json);
            Console.WriteLine(soap.current_location.city);
        }
    }
    

    affiliations 属性而言,我看不到它与您的JSON 有任何关系,所以我只是将它从Data 对象中删除并替换为EducationHistory 类型。


    更新:

    要处理多个节点,您可以调整模型:

    public class Result
    {
        public Data[] data { get; set; }
    }
    
    public class Data
    {
        public CurrentLocation current_location { get; set; }
        public WorkHistory[] work_history { get; set; }
        public EducationHistory[] education_history { get; set; }
    }
    
    public class EducationHistory
    {
        public string name { get; set; }
        public int? year { get; set; }
        public string school_type { get; set; }
    }
    
    public class WorkHistory
    {
        public string company_name { get; set; }
        public Location location { get; set; }
    }
    
    public class CurrentLocation
    {
        public string city { get; set; }
        public string state { get; set; }
        public string country { get; set; }
    }
    
    public class Location
    {
        public string city { get; set; }
        public string state { get; set; }
    }
    

    然后:

    Result soap = JsonConvert.DeserializeObject<Result>(json);
    Console.WriteLine(soap.data[0].current_location.city);
    

    【讨论】:

    • @vini,哪个属性为空?我已经用你在问题中显示的 JSON 强测试了代码,它工作得很好。
    • @vini,抱歉,我无法重现您的问题。我已经更新了我的答案以提供一个完整的工作示例。
    • 如果我只有一个用户,提供的解决方案会起作用,但是我有几个用户拥有这些详细信息,因此它不起作用:/
    • @vini,你是什么意思?你的 JSON 是什么样子的?
    • 说如果我有你再次提供的相同 JSON 代码,输出还会出现吗?我的情况就是这样
    【解决方案3】:

    这是另一个用于检索 id、姓名、图片和朋友的示例。您必须包含对 newtonsoft.json 包的引用。

    [TestClass]
    public class FbRequestParserTests
    {
        [TestMethod]
        public void ParseFacebookResponse()
        {
            var response =
                @"{
                  ""id"": ""123123"",
                  ""name"": ""My name"",
                  ""picture"": {
                                ""data"": {
                                    ""is_silhouette"": false,
                                    ""url"": ""https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/14572785_123""
                                }
                            },
                  ""friends"": {
                    ""data"": [
                        {
                        ""name"": ""Friend 1"",
                        ""id"": ""123""
                      },
                      {
                        ""name"": ""Friend 2"",
                        ""id"": ""234""
                      }
                    ],
                    ""paging"": {
                      ""cursors"": {
                        ""before"": ""QVFIUk1yRE9zTkhFdkc1TFV2SHVpaE1IYUJ4V2ljbUJSbjYxaGhnX05IcTNYWHp0ekNHZAnh6LThs"",
                        ""after"": ""QVFIUjFFQW5kVmJiTmMxZAHN6cWNDdUl5VnVJVl91UG0yV2hMOVl5N21GTDVxQ2JVQ2hjQVlRYXBxNDNkWWI3YjZAkZAFBV""
                      }
                    },
                    ""summary"": {
                      ""total_count"": 456
                    }
                  }
                }";
    
            var result = JsonConvert.DeserializeObject<FbResponse>(response);
    
            Assert.AreEqual(result.Id, 123123);
            Assert.AreEqual(result.Name, "My Name");
        }
    }
    
    public class FbResponse
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public Picture Picture { get; set; }
        public Friends Friends { get; set; }
    }
    public class Friends
    {
        public List<FriendsData> Data { get; set; }
        public Paging Paging { get; set; }
        public Summary Summary { get; set; }
    }
    public class Summary
    {
        public int Total_Count { get; set; }
    }
    public class Paging
    {
        public Cursors Cursors { get; set; }
    }
    public class Cursors
    {
        public string Before { get; set; }
        public string After { get; set; }
    }
    public class FriendsData
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    public class Picture
    {
        public PictureData Data { get; set; }
    }
    public class PictureData
    {
        public bool Is_Silhouette { get; set; }
        public string Url { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-22
      • 2023-03-23
      • 2021-09-02
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多