Json 反序列化忽略具有错误值的对象

Json deserialize ignoring objects with wrong values

我想让我的 json 反序列化器忽略具有不正确值的对象(例如字符串代替 int)或 return null 并继续反序列化 [=24 的其余部分=] 文件.

这是我的 json:

{
    "requests":[
        {
            "clientId":"1",
            "requestId":"1",
            "name":"Bułka",
            "quantity":"1",
            "price":"10.00"
        },
        {
            "clientId":"1",
            "requestId":"2.1",
            "name":"Chleb",
            "quantity":"2",
            "price":"15.00"
        },
        {
            "clientId":"1",
            "requestId":"2",
            "name":"Chleb",
            "quantity":"5",
            "price":"15.00"
        },
        {
            "clientId":"2",
            "requestId":"1",
            "name":"Chleb",
            "quantity":"1",
            "price":"10.00"
        }
    ]
}

这是我要反序列化的 类:

class RequestCollection
{
    public List<Request> requests { get; set; }

    public RequestCollection()
    {
        requests = new List<Request>();
    }
}

class Request
{
    public string clientId { get; set; }
    public long requestId { get; set; }
    public string name { get; set; }
    public int quantity { get; set; }
    public double price { get; set; }

    public Request() { }
    public Request(string clientID, long requestID, string name, int quantity, double price)
    {
        this.clientId = clientID;
        this.requestId = requestID;
        this.name = name;
        this.quantity = quantity;
        this.price = price;
    }
}

这是我反序列化文件的方式:

requestCollectionLocal = JsonConvert.DeserializeObject<RequestCollection>(json);

如您所见,json 文件中第二个对象的 requestId 值不正确。我希望反序列化的结果只是 3 个其他对象或所有 4 个具有空值的对象,而不是不正确的值。

你可以将你的 json 反序列化为匿名类型,然后使用 linq

过滤结果

像这样定义一个匿名类型

var template = new {
  requests = new [] {
    new {
      clientId = "",
      requestId = "",
      name = "",
      quantity = "",
      price = ""
    } 
  }
};

这里可以看到所有的类型都是字符串,所以反序列化应该可以正常工作。现在您应该检查是否可以使用 TryParse 将每个字符串转换为相应的数据类型。完整代码:

var json = "{\r\n    \"requests\":[\r\n        {\r\n            \"clientId\":\"1\",\r\n            \"requestId\":\"1\",\r\n            \"name\":\"Bułka\",\r\n            \"quantity\":\"1\",\r\n            \"price\":\"10.00\"\r\n        },\r\n        {\r\n            \"clientId\":\"1\",\r\n            \"requestId\":\"2.1\",\r\n            \"name\":\"Chleb\",\r\n            \"quantity\":\"2\",\r\n            \"price\":\"15.00\"\r\n        },\r\n        {\r\n            \"clientId\":\"1\",\r\n            \"requestId\":\"2\",\r\n            \"name\":\"Chleb\",\r\n            \"quantity\":\"5\",\r\n            \"price\":\"15.00\"\r\n        },\r\n        {\r\n            \"clientId\":\"2\",\r\n            \"requestId\":\"1\",\r\n            \"name\":\"Chleb\",\r\n            \"quantity\":\"1\",\r\n            \"price\":\"10.00\"\r\n        }\r\n    ]\r\n}";

var template = new { requests = new [] { new {clientId = "", requestId = "", name = "", quantity = "", price = ""} }};
var tempRequestCollection = JsonConvert.DeserializeAnonymousType(json, template);

var result = new RequestCollection
{
    requests = tempRequestCollection.requests
        .Where(r => 
            long.TryParse(r.requestId, out var _)
            && int.TryParse(r.quantity, out var _)
            && double.TryParse(r.price, out var _)
          )
        .Select(r => new Request
        {
            clientId = r.clientId,
            requestId = long.Parse(r.requestId),
            name = r.name,
            quantity = int.Parse(r.quantity),
            price = double.Parse(r.price)
        })
        .ToList()
};