Fourr Kositanont
Fourr Kositanont

Reputation: 23

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?

Upvotes: 2

Views: 5689

Answers (3)

Mayer Spitz
Mayer Spitz

Reputation: 2525

For some people this might be helpful:

Add Namespace: using Newtonsoft.Json;

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

var json = JsonConvert.DeserializeObject(jsonString);            
return Ok(json);

Upvotes: 0

user3486773
user3486773

Reputation: 1246

Download the Json as a string, and use something like myString = myString.Replace(@"High School", "HighSchool"). Do this as the step before deserializing.

Upvotes: 0

Sean Kenny
Sean Kenny

Reputation: 1636

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.

Upvotes: 12

Related Questions