Reputation: 1
I have been working with JSON for years but I have never been faced with a situation where the key values are dynamic. I am sure there are other situations but in my particular case I am trying to deserialize a LinkedIn stream. Following is a sample of the data returned request that is made with the scope set to r_liteprofile.
I have tried using a Dictionary but the return value is always null.
Following is a sample of the data return from the LinkedIn API call:
{
"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"
}
}
What I don’t understand is how to handle “localize” when the data is being deserialized. If I anticipate that the key value is going to be “en_US” everything works fine. But this is not something that will always be true. From my research the number of different values is limited and the following is a subset of the available keys.
{
"localized": {
"de_DE": "LinkedIn",
"en_US": "LinkedIn",
"es_ES": "LinkedIn",
"in_ID": "LinkedIn"
}
}
Following is the type definition:
public class Localized
{
public string en_Us { get; set; }
}
public class PreferredLocale
{
public string country { get; set; }
public string language { get; set; }
}
public class LastName
{
public Localized localized { get; set; }
public PreferredLocale preferredLocale { get; set; }
}
public class FirstName
{
public Localized localized { get; set; }
public PreferredLocale preferredLocale { get; set; }
}
public class ProfilePicture
{
public string displayImage { get; set; }
}
public class LinkedLiteProfile
{
public LastName lastName { get; set; }
public FirstName firstName { get; set; }
public ProfilePicture profilePicture { get; set; }
public string id { get; set; }
}
And the code:
var data = System.IO.File.ReadAllText(<<file address>>);
var result = JsonConvert.DeserializeObject<LinkedLiteProfile>(data);
Upvotes: 0
Views: 816
Reputation: 1
To extract the value I used the following
var data = System.IO.File.ReadAllText(<<file address>>);
var result = JsonConvert.DeserializeObject<LinkedLiteProfile>(data);
var firstName = result.firstName.localized.Values.FirstOrDefault();
Upvotes: 0
Reputation: 1
I figured it had to be something simple. I had tried replacing the setter with
public Dictionary<string, string> Localized {get; set; }
but the result was null.
What worked for me (as suggested by Joao Paulo Amorim) was to replace Localized with a Dictionary as follows:
public class LastName
{
public Dictionary<string, string> localized { get; set; }
public PreferredLocale preferredLocale { get; set; }
}
public class FirstName
{
public Dictionary<string, string> localized { get; set; }
public PreferredLocale preferredLocale { get; set; }
}
Upvotes: 0
Reputation: 652
You could parse it into a Dictionary, by replacing your class Localised
with:
public Dictionary<string,string> Localised { get; set; }
Newtonsoft.JSON and Json.NET can automatically parse these objects into a dictionary.
Upvotes: 1