如何创建一个基础 JSON 模式 class 来处理自定义数据类型作为响应?
How to create a base JSON schema class which handle custom data type in response?
我正在创建一个库包装器来处理 this 足球网站的所有 API
请求。
当我调用端点时,返回此响应结构:
{
"get": "countries",
"parameters": [],
"errors": [],
"results": 161,
"paging": {
"current": 1,
"total": 1
},
"response": [
{
"name": "Albania",
"code": "AL",
"flag": "https://media.api-sports.io/flags/al.svg"
},
{
"name": "Algeria",
"code": "DZ",
"flag": "https://media.api-sports.io/flags/dz.svg"
},
{
"name": "Andorra",
"code": "AD",
"flag": "https://media.api-sports.io/flags/ad.svg"
},
]
}
上面的示例是 /countries
端点。我已经使用 Quicktype 生成模式结构,我得到了这个:
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace FootballAPI.Models
{
public partial class Temperatures
{
[JsonProperty("get")]
public string Get { get; set; }
[JsonProperty("parameters")]
public object[] Parameters { get; set; }
[JsonProperty("errors")]
public object[] Errors { get; set; }
[JsonProperty("results")]
public long Results { get; set; }
[JsonProperty("paging")]
public Paging Paging { get; set; }
[JsonProperty("response")]
public Response[] Response { get; set; }
}
public partial class Paging
{
[JsonProperty("current")]
public long Current { get; set; }
[JsonProperty("total")]
public long Total { get; set; }
}
public partial class Response
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("flag")]
public Uri Flag { get; set; }
}
}
我想要做的是有一个基本结构模式,其中包含除 response
属性 之外的所有内容。在上面的示例中,response
必须是 Country
的一个实例,即:
public class Country
{
public string name { get; set; }
public string code { get; set; }
public string flag { get; set; }
}
我如何告诉上面的基本模式使用 Country
作为 T
对象?
我应该能够使用 Newtonsoft.JSON
:
正确解析所有内容
JsonConvert.DeserializeObject<Country>;
我有不同的方法,比如 GetCountries()
,它只传递查询字符串,在那种情况下是 /countries
。所以我知道当时我希望 "response"
的具体类型。
试试这些 类
public class Country
{
public string name { get; set; }
public string code { get; set; }
public string flag { get; set; }
}
public class Countries
{
public string get { get; set; }
public List<object> parameters { get; set; }
public List<object> errors { get; set; }
public int results { get; set; }
public Paging paging { get; set; }
public List<Country> response { get; set; }
}
public class Paging
{
public int current { get; set; }
public int total { get; set; }
}
你可以使用
JsonConvert.DeserializeObject<Countries>(json);
我正在创建一个库包装器来处理 this 足球网站的所有 API
请求。
当我调用端点时,返回此响应结构:
{
"get": "countries",
"parameters": [],
"errors": [],
"results": 161,
"paging": {
"current": 1,
"total": 1
},
"response": [
{
"name": "Albania",
"code": "AL",
"flag": "https://media.api-sports.io/flags/al.svg"
},
{
"name": "Algeria",
"code": "DZ",
"flag": "https://media.api-sports.io/flags/dz.svg"
},
{
"name": "Andorra",
"code": "AD",
"flag": "https://media.api-sports.io/flags/ad.svg"
},
]
}
上面的示例是 /countries
端点。我已经使用 Quicktype 生成模式结构,我得到了这个:
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace FootballAPI.Models
{
public partial class Temperatures
{
[JsonProperty("get")]
public string Get { get; set; }
[JsonProperty("parameters")]
public object[] Parameters { get; set; }
[JsonProperty("errors")]
public object[] Errors { get; set; }
[JsonProperty("results")]
public long Results { get; set; }
[JsonProperty("paging")]
public Paging Paging { get; set; }
[JsonProperty("response")]
public Response[] Response { get; set; }
}
public partial class Paging
{
[JsonProperty("current")]
public long Current { get; set; }
[JsonProperty("total")]
public long Total { get; set; }
}
public partial class Response
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("flag")]
public Uri Flag { get; set; }
}
}
我想要做的是有一个基本结构模式,其中包含除 response
属性 之外的所有内容。在上面的示例中,response
必须是 Country
的一个实例,即:
public class Country
{
public string name { get; set; }
public string code { get; set; }
public string flag { get; set; }
}
我如何告诉上面的基本模式使用 Country
作为 T
对象?
我应该能够使用 Newtonsoft.JSON
:
JsonConvert.DeserializeObject<Country>;
我有不同的方法,比如 GetCountries()
,它只传递查询字符串,在那种情况下是 /countries
。所以我知道当时我希望 "response"
的具体类型。
试试这些 类
public class Country
{
public string name { get; set; }
public string code { get; set; }
public string flag { get; set; }
}
public class Countries
{
public string get { get; set; }
public List<object> parameters { get; set; }
public List<object> errors { get; set; }
public int results { get; set; }
public Paging paging { get; set; }
public List<Country> response { get; set; }
}
public class Paging
{
public int current { get; set; }
public int total { get; set; }
}
你可以使用
JsonConvert.DeserializeObject<Countries>(json);