我正在尝试从API反序列化一个JSON字符串。
除了嵌套项-位置(Location)始终为空之外,所有工作都正常。
这是字符串的结构:
[
{
"CompanyProfile": "<p>A&B Inc etc...</p>",
"Address": "56 Test Street, Test, UK",
"Location": {
"Latitude": 61.52787,
"Longitude": -4.32095,
"Zoom": 13,
"Icon": null,
"Format": 0,
"Api": null,
"SearchTyped": null
},
"Id": 1723,
"Name": "A&B Inc"
},
{
"CompanyProfile": "<p>B&C Inc etc...</p>",
"Address": "57 Test Street, Test, UK",
"Location": {
"Latitude": 61.2122,
"Longitude": -4.31111,
"Zoom": 13,
"Icon": null,
"Format": 0,
"Api": null,
"SearchTyped": null
},
"Id": 1723,
"Name": "B&C Inc"
},
]
以下是需要映射的类:
public class MemberDto
{
public int Id { get; set; }
public string? Name { get; set; }
public string? CompanyProfile { get; set; }
public string? Address { get; set; }
public Location? Location { get; internal set; }
}
public class Location
{
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
}
这是反序列化代码:
var result = await response.Content.ReadAsStringAsync();
var members = JsonConvert.DeserializeObject<List<MemberDto>>(result);
我知道我也可以使用ReadFromJsonAsync<List<MemberDto>>()
,但是使用ReadFromString可以在反序列化之前检查JSON。无论如何,对于ReadFromJsonAsync结果完全相同。
除了Location外,所有内容都成功反序列化了。
有人知道问题是什么吗?