如何访问名称中包含空格的Json Attribute

问题描述 投票:2回答:3

在下面找到json的回复......

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

}

我的班级在这里

    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; }
}

然后我把代码:

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

由于Attribute有空间,它不起作用。我该怎么办?

c# json windows-phone
3个回答
9
投票

尝试添加JsonProperty属性。这对你有用。

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

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

编辑

我看到你也有尾随空格,所以更新了上面的属性。对“名称”等做同样的事情。


0
投票

将Json下载为字符串,并使用类似myString = myString.Replace(@“High School”,“HighSchool”)的内容。这是反序列化之前的步骤。


0
投票

对某些人来说,这可能会有所帮助:

添加命名空间: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);
© www.soinside.com 2019 - 2024. All rights reserved.