Anna Dmytriv
Anna Dmytriv

Reputation: 9

DeserializeObject in .net only for specific properties

I have a class with 2 properties, but I'm getting a JSON object with a few properties that I don't need. I want to use JsonConvert.DeserializeObject but I'm getting an error because my class doesn't correspond to the JSON object. Do you have any code example on how to solve this using JsonConvert? I want to avoid using JObject.Parse

How I tried to use JsonConvert:

 var data = JsonConvert.DeserializeObject<LinkedInUser>(await GetData(token));

GetData returns the JSON object

LinkedInUseris the class that contains the properties that exist in the JSON object that i need

edit: I'm trying to get data from linkedin

This is the Json object:

   {
   "id":"REDACTED",
   "firstName":{
      "localized":{
         "en_US":"Tina"
      },
      "preferredLocale":{
         "country":"US",
         "language":"en"
      }
   },
   "lastName":{
      "localized":{
         "en_US":"Belcher"
      },
      "preferredLocale":{
         "country":"US",
         "language":"en"
      }
   },
    "profilePicture": {
        "displayImage": "urn:li:digitalmediaAsset:B54328XZFfe2134zTyq"
    }
}

And this is LinkedInUser object:

namespace project.DTOs.Models
{
    public class LinkedInUser
    {
        public string FirstName { get; set;}
        public string LastName { get; set; }  
    }
}

I can't convert the JSON to LinkedInUser using JsonConvert.DeserializeObject this way, does anyone know a way to do it easily?

Upvotes: 0

Views: 542

Answers (1)

Serge
Serge

Reputation: 43870

I can only offer you this solution in one line using modernized LinkedInUser

LinkedInUser person = JsonConvert.DeserializeObject<LinkedInUser>(json);

or in two lines, using your LinkedInUser

var data = JsonConvert.DeserializeObject<Data>(json);
LinkedInUser user = new LinkedInUser { FirstName=data.FirstName, LastName=data.LastName};

output

FirstName   Tina
LastName    Belcher

but you will not need any extra classes at all if you use Parse

    var o =JObject.Parse(json);

    var linkedInUser = new LinkedInUser {
         FirstName=  (string) o["firstName"]["localized"]["en_US"],
         LastName= (string) o["lastName"]["localized"]["en_US"]
         };

classes

public class Data:BaseData
// or
public class LinkedInUser:BaseData
{
    [JsonIgnore]
    public string FirstName
    {
        get { return _stFirstName == null ? null : _stFirstName.Localized.EnUs; }
    }

    [JsonIgnore]
    public string LastName
    {
        get { return _stLastName == null ? null : _stLastName.Localized.EnUs; }
    }
}


public partial class BaseData
{
    protected StName _stFirstName { get; set; }
    protected StName _stLastName { get; set; }

    [JsonProperty("firstName")]
    public StName StFirstName
    {
        set { _stFirstName = value; }
    }

    [JsonProperty("lastName")]
    public StName StLastName
    {
        set { _stLastName = value; }
    }
}

public partial class StName
{
    [JsonProperty("localized")]
    public Localized Localized { get; set; }
}

public partial class Localized
{
    [JsonProperty("en_US")]
    public string EnUs { get; set; }
}

Upvotes: 1

Related Questions