【发布时间】:2019-01-05 22:43:10
【问题描述】:
我有以下 JSON 对象:
{
"entity": "Customer",
"id": "XXX",
"isActive": "1",
"createdTime": "2018-01-30T18:56:51+02:00",
"updatedTime": "2019-01-05T02:15:17+02:00",
以及以下 DTO 类:
public class Customer
{
public string Id { get; set; }
public bool IsActive { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
}
和反序列化代码:
var jsonResponse = JObject.Parse(customerString);
JToken array = jsonResponse["response_data"];
List<Customer> result = (List<Customer>)array.ToObject(typeof(List<Customer>));
但我对IsActive 属性有疑问,因为 json 对象有 1 或 0 个值,但我想要布尔值。怎么写JsonSerializer来做呢?
【问题讨论】:
-
看起来像 Convert an int to bool with Json.Net 或 JSonNet boolean serialization 的副本。同意吗?
-
你说得对,谢谢
标签: c# json.net jsonserializer