json避免忽略具有错误值的对象



我想让我的JSON DESERIALIZER忽略具有不当值的对象(例如字符串代替INT)或为它们返回null,并继续对JSON文件的其余部分进行验证。

这是我的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 = "{rn    "requests":[rn        {rn            "clientId":"1",rn            "requestId":"1",rn            "name":"Bułka",rn            "quantity":"1",rn            "price":"10.00"rn        },rn        {rn            "clientId":"1",rn            "requestId":"2.1",rn            "name":"Chleb",rn            "quantity":"2",rn            "price":"15.00"rn        },rn        {rn            "clientId":"1",rn            "requestId":"2",rn            "name":"Chleb",rn            "quantity":"5",rn            "price":"15.00"rn        },rn        {rn            "clientId":"2",rn            "requestId":"1",rn            "name":"Chleb",rn            "quantity":"1",rn            "price":"10.00"rn        }rn    ]rn}";
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()
};

相关内容

  • 没有找到相关文章

最新更新