2

在下面的 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);

它不起作用,因为 Arttribute 有空间。我该怎么办?

4

3 回答 3

11

尝试添加JsonProperty属性。那应该对你有用。

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

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

编辑

我看到你也有尾随空格,所以更新了上面的属性。对“名称”等执行相同的操作。

于 2013-06-07T11:15:11.160 回答
0

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

于 2017-03-17T15:57:08.837 回答
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);
于 2019-02-12T20:51:00.233 回答