Search code examples
c#jsonwindows-phone

How to access Json Attribute which has space in its name


Find below the json response...

{
"personalDetails": {
    "Name ": " Taeyeon",
    "Date Of Birth ": " 03/09/1989",
    "Zodiac ": " Pisces"
},
"education": {
    "High School ": " Jeonju Art High school ",
    "University ": " -"
}

}

My Class is here

    public class Biography
{
    public personalDetails personalDetails { get; set; }
    public education education { get; set; }
    public work work { get; set; }
    public personal personal { get; set; }
}


public class personalDetails
{
    public string Name { get; set; }
    public string DateBirth { get; set; }
    public string Zodiac { get; set; }
}

public class education
{
    public string HighSchool { get; set; }
    public string University { get; set; }
}

Then I put the code:

Biography dataSet = JsonConvert.DeserializeObject<Biography>(e.Result);

It doesn't work because of Arttribute has space. What should I do?


Solution

  • Try adding the JsonProperty attribute. That should work for you.

    [JsonProperty(PropertyName = "Date Of Birth ")]
    public string DateBirth { get; set; }
    
    [JsonProperty(PropertyName = "High School ")]
    public string HighSchool { get; set; }
    

    Edit

    I see you have trailing spaces too so updated the attributes above. Do the same for "Name ", etc.