Facebook的JSON字符串的反序列化? [英] Deserialization of a Facebook JSON string?

查看:250
本文介绍了Facebook的JSON字符串的反序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能让我的头周围提取后,我跑我的FQL查询所生成的JSON字符串Facebook用户的work_history,隶属关系和current_loc

I am not able to get my head around extracting the work_history, affiliations and current_loc of the facebook user from the JSON string that is generated after i run my fql query

我已经做这个类:

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字符串的格式为:

The format of my JSON string is :

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

我如何提取此数据我得到一个空当我使用这个code:

How do i extract this Data i get a null when i use this code :

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

编辑:我就照改变code什么答案建议我还是得不到值

Edit : I did change the code according to what an answer suggested i still get no values

编辑2:的提供会工作,如果我只有一个用户解决方案,但我已经有这些细节的几个用户如何获得这些数据?有几个像为不同的用户下面给出这些值

Edit 2: the solution that is provided would have worked if i had only one user however i have several users having these details how do i get this data? There are several like these values given below for different users

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

请帮忙

推荐答案

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

You need to use correct property names that match your JSON structure:

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);
    }
}

至于的隶属关系属性而言,我没有看到任何关于它与你JSON,所以我只是从数据对象中删除并替换它与 EducationHistory 键入

As far as the affiliations property is concerned, I don't see any relation to it with your JSON, so I simply removed it from the Data object and replaced it with the EducationHistory type.

更新:

和处理多个节点,你可以调整你的模型:

And to handle multiple nodes you could adapt your models:

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);

这篇关于Facebook的JSON字符串的反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆