【发布时间】:2020-02-25 23:40:52
【问题描述】:
我正在编写一个用于使用 UptimeRobot API 的 .Net 标准库。我发现一个端点有时会返回 int 的数组,有时会返回单个 0。
我正在使用的字段是monitors。
{
"stat": "ok",
"pagination": {
"offset": 0,
"limit": 50,
"total": 2
},
"psps": [
{
"id": 2345678,
"friendly_name": "Full Status",
"monitors": 0,
"sort": 1,
"status": 1,
"standard_url": "https://stats.uptimerobot.com/xxxxxx",
"custom_url": ""
},
{
"id": 123456,
"friendly_name": "Filtered Status",
"monitors": [
783210435,
783210481
],
"sort": 1,
"status": 1,
"standard_url": "https://stats.uptimerobot.com/xxxxxx",
"custom_url": ""
}
]
}
这是我的模型:
using Newtonsoft.Json;
using System.Collections.Generic;
using SharpenUp.Common.Types;
using System.Diagnostics.CodeAnalysis;
namespace SharpenUp.Common.Models.PublicStatusPages
{
public class PublicStatusPage
{
[ExcludeFromCodeCoverage]
[JsonProperty( PropertyName = "id" )]
public int Id { get; set; }
[JsonProperty( PropertyName = "friendly_name" )]
public string Name { get; set; }
[JsonProperty( PropertyName = "monitors" )]
public List<int> Monitors { get; set; }
[JsonProperty( PropertyName = "sort" )]
public int Sort { get; set; }
[JsonProperty( PropertyName = "status" )]
public PublicStatusPageStatusType Status { get; set; }
[JsonProperty( PropertyName = "standard_url" )]
public string StandardURL { get; set; }
[JsonProperty( PropertyName = "custom_url" )]
public string CustomURL { get; set; }
}
}
如果我将查询限制为数组的结果,Monitors 对象将解析,但如果我尝试引入另一个则失败。
有什么想法吗?我什至不能 100% 确定使用什么措辞来尝试解决这个问题。
【问题讨论】:
-
添加一个转换器以检查是否返回单个 0 并将其添加到列表中并为其他所有内容返回默认值。