RestSharp - 反序列化 json 具有无效键名的响应(包含句点)
RestSharp - deserialize json response with invalid key name (contains a period )
我已经坚持了一段时间。我有一个 JSON 响应向我发送包含句点的密钥。例如:"cost_center.code"
如何将它放入我的对象中?我没有收到任何错误,但该值只是以 null 形式出现,并且没有被反序列化到我的 class.
这是我的 classes:
public class Result
{
public string company { get; set; }
public string first_name { get; set; }
public string email { get; set; }
public string employee_id { get; set; }
public string last_name { get; set; }
[DeserializeAs(Name="cost_center.code")]
public string cost_center { get; set; }
}
public class RootObject
{
public List<Result> result { get; set; }
}
这是 JSON 回复:
{
"result": [
{
"company": "My Company",
"first_name": "First",
"email": "example@fakeaddress.com",
"employee_id": "123456789",
"last_name": "Last",
"cost_center.code": "12345"
}
]
}
我执行:
var response = client.Execute<List<RootObject>>(request);
// this returns null
Console.WriteLine(response.Data[0].result[0].cost_center);
// all other values return fine ex:
Console.WriteLine(response.Data[0].result[0].company);
我已经尝试过使用 DeserializeAs 和不使用 DeserializeAs。我不确定它是否有效。我使用这个 属性 不正确吗?是列表的容器问题吗?
编辑并接受以下答案以使用 JsonProperty。对于可能出现的其他人来说,这就是解决方案。
添加了 JSON.net nuget。
using Newtonsoft.Json;
按照说明设置 JsonProperty:
[JsonProperty("cost_center.code")]
将我的执行更改为:
var response = client.Execute(request);
然后像这样反序列化它:
var jsonResponse = JsonConvert.DeserializeObject<RootObject>(response.Content);
之后我可以访问值:
Console.WriteLine(jsonResponse.result[0].CostCenter
对名称中包含句点的属性执行以下操作:
[JsonProperty("cost_center.code")]
public string CostCenter{ get; set; }
它应该有效
如果您想本地使用 RestSharp 或无法获得 Newtonsoft.Json.JsonSerializer 支持(我做不到),他们只是添加了对适当的支持自 106.1.0.
起对名称中带点的属性进行反序列化
在这里查看我的回复:
我已经坚持了一段时间。我有一个 JSON 响应向我发送包含句点的密钥。例如:"cost_center.code"
如何将它放入我的对象中?我没有收到任何错误,但该值只是以 null 形式出现,并且没有被反序列化到我的 class.
这是我的 classes:
public class Result
{
public string company { get; set; }
public string first_name { get; set; }
public string email { get; set; }
public string employee_id { get; set; }
public string last_name { get; set; }
[DeserializeAs(Name="cost_center.code")]
public string cost_center { get; set; }
}
public class RootObject
{
public List<Result> result { get; set; }
}
这是 JSON 回复:
{
"result": [
{
"company": "My Company",
"first_name": "First",
"email": "example@fakeaddress.com",
"employee_id": "123456789",
"last_name": "Last",
"cost_center.code": "12345"
}
]
}
我执行:
var response = client.Execute<List<RootObject>>(request);
// this returns null
Console.WriteLine(response.Data[0].result[0].cost_center);
// all other values return fine ex:
Console.WriteLine(response.Data[0].result[0].company);
我已经尝试过使用 DeserializeAs 和不使用 DeserializeAs。我不确定它是否有效。我使用这个 属性 不正确吗?是列表的容器问题吗?
编辑并接受以下答案以使用 JsonProperty。对于可能出现的其他人来说,这就是解决方案。
添加了 JSON.net nuget。
using Newtonsoft.Json;
按照说明设置 JsonProperty:
[JsonProperty("cost_center.code")]
将我的执行更改为:
var response = client.Execute(request);
然后像这样反序列化它:
var jsonResponse = JsonConvert.DeserializeObject<RootObject>(response.Content);
之后我可以访问值:
Console.WriteLine(jsonResponse.result[0].CostCenter
对名称中包含句点的属性执行以下操作:
[JsonProperty("cost_center.code")]
public string CostCenter{ get; set; }
它应该有效
如果您想本地使用 RestSharp 或无法获得 Newtonsoft.Json.JsonSerializer 支持(我做不到),他们只是添加了对适当的支持自 106.1.0.
起对名称中带点的属性进行反序列化在这里查看我的回复: