C# 在Json模型中为动态JsonProperty名称正确使用自定义转换器
我正在处理一些Json数据,这些数据有一个AppID字段。显然,这个AppID会根据我查询的应用程序而改变。所以在网上搜索时,我发现我可以使用自定义转换器来完成这项任务,但我的实现似乎失败了,我不知道为什么。特别是当试图打印从Json反序列化的数据返回null时,它会失败。我的模型(删减)如下C# 在Json模型中为动态JsonProperty名称正确使用自定义转换器,c#,json,.net,api,steam-web-api,C#,Json,.net,Api,Steam Web Api,我正在处理一些Json数据,这些数据有一个AppID字段。显然,这个AppID会根据我查询的应用程序而改变。所以在网上搜索时,我发现我可以使用自定义转换器来完成这项任务,但我的实现似乎失败了,我不知道为什么。特别是当试图打印从Json反序列化的数据返回null时,它会失败。我的模型(删减)如下 public partial class SteamAPIModel { [JsonProperty("AppIDJson")] [JsonConverter(typeo
public partial class SteamAPIModel
{
[JsonProperty("AppIDJson")]
[JsonConverter(typeof(AppIDConverter))]
public AppIDJson AppIDJson { get; set; }
}
public partial class AppIDJson
{
[JsonProperty("success")]
public bool success { get; set; }
public Data data { get; set; }
}
public partial class Data
{
public string name { get; set; }
public string short_description { get; set; }
}
public class AppIDConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(AppIDJson);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//This int value here is declared in another class and accessed this way.
return apiTask.ApplicationIDAPITask;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
就我所知,对于这个模型,除了从另一个类访问了我希望的实际属性名之外,它看起来都是标准的。下面声明了该类,为了清晰起见,我将包括我的API调用
public class apiTask
{
public static int ApplicationIDAPITask { get; set; }
public static void Runner(int ApplicationIDSource)
{
ApplicationIDAPITask = ApplicationIDSource;
RunAsync().Wait();
}
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://store.steampowered.com/api/appdetails/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//in the below URL I do have a token in the final project to allow the number of queries needed for the project
HttpResponseMessage response = await client.GetAsync("&appids="+ ApplicationIDAPITask).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
string httpResponseBody = await response.Content.ReadAsStringAsync();
SteamAPIModel game_item = JsonConvert.DeserializedObject<SteamAPIModel>(httpResponseBody);
if (game_item.AppIDJson != null)
{
Debug.WriteLine(game_item.AppIDJson.success.ToString());
} else
{
//This is what executes currently, never completing the code in the if statement,
}
}
}
}
}
目前,如果我改为将“AppIDJson”标记为特定的AppID,并对该特定的AppID进行查询,那么所有内容都会按预期工作,因此此代码的其余部分也会工作,我只想将其包括在内,以帮助解决我的问题。感谢您在这个问题上提供的帮助,如果您注意到我的代码中还有什么可以改进的地方,因为在这个项目中,这里几乎所有的东西对我来说都是新的。您真的需要这个转换器吗?Newtonsoft JSON.net支持字典反序列化:您可以从响应中反序列化
IDictionary
,其中string Key
是AppIDI老实说,我不确定我是否这样做了,这只是我当时找到的一个看似解决方案。但是谢谢你,我将继续尝试使用反序列化。
{
"730": {
"success": true,
"data": {
"type": "game",
"name": "Counter-Strike: Global Offensive",
"steam_appid": 730,
"required_age": 0,
"is_free": true,
"controller_support": "full",
..........