我正在尝试从api反序列化json字符串。
所有工作,除了嵌套项- Location总是以null结束
这是字符串结构:
[
{
"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之外的所有内容都成功反序列化了
有人知道是什么问题吗?
删除Location setter中的内部访问修饰符
public Location? Location { get; set; }