locked
How to deserialize a JSON string from facebook in asp.net? RRS feed

  • Question

  • User1527256921 posted

    I am having real hard time trying to deserialize this JSON string where in i want to extract the user's work_history, current_location and education_history

    My string is a as follows:

    {"uid":56354665,"name":"ABCBGFF","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":null,"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"}],"contact_email":null} ,
    {"uid":522036860,"name":"ABCPRE","pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-snc4\/174460_522036860_1780781144_q.jpg","affiliations":[],"birthday_date":"12\/07","sex":"female","relationship_status":null,"current_location":{"city":"New Delhi","state":"Delhi","country":"India","zip":"","id":106517799384578,"name":"New Delhi, India"},"education_history":[{"name":"Amity University","concentrations":["Event Management","Journalism & Mass Communication","Advertising"],"school_type":"College"},{"name":"Amity University","year":2009,"concentrations":["Journalism and Mass Communication"],"school_type":"College"},{"name":"Amity School of Communication","concentrations":[],"school_type":"College"}],"work_history":[{"location":{"city":"","state":""},"company_name":"- \/ as a photographer! :D","description":"","start_date":"0000-00"},{"location":{"city":"","state":""},"company_name":"Infinity Advertising Services Pvt. Ltd.","description":"","start_date":"0000-00","end_date":"0000-00"}],"contact_email":null }
    
    
    My Class is as follows :
    public class Data
    {
    public List<CurrentLocation> current_location { get; set; }
    public List<WorkHistory> work_history { get; set; }
    public List<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; }
    }

    How do i retreive and display values currently its showing null values 

    when i try this :
    
    
    
     Data soap = JsonConvert.DeserializeObject<Data>(ser);
    Sunday, February 19, 2012 1:31 AM

Answers

  • User933690637 posted

    HI,

    You can call overloaded method of DeserializedObject as 

    DeserializeObject<T>(string value, JsonSerializerSettings settings);
    and set setting as (whichever is your case, null handling, default value etc)
     JsonSerializerSettings setting = new JsonSerializerSettings();
    setting.DefaultValueHandling = DefaultValueHandling.Ignore;
    setting.MissingMemberHandling = MissingMemberHandling.Ignore;
    setting.NullValueHandling = NullValueHandling.Include;
    and call 
    JsonConvert.DeserializeObject<Data>(ser,setting); method
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 7, 2012 2:09 AM