Razor Pages中Riot API反序列化后属性未赋值问题求助
问题:Riot API反序列化召唤师信息失败,所有属性未赋值
我尝试使用Riot API读取召唤师信息,但反序列化完成后所有属性值均未被赋值。以下是模型定义和API调用方法:
模型代码
public class LOLSummoner { [JsonProperty("id")] public string id { get; set; } [JsonProperty("accountId")] public string accountId { get; set; } [JsonProperty("puuid")] public string puuid { get; set; } [JsonProperty("name")] public string name { get; set; } [JsonProperty("profileIconId")] public string profileIconId { get; set; } [JsonProperty("revisionDate")] public string revisionDate { get; set; } [JsonProperty("summonerLevel")] public string summonerLevel { get; set; } }
API调用方法
public async void OnPostCallAPI() { string Baseurl = "https://eun1.api.riotgames.com/lol/summoner/v4/summoners/by-name/PolskaHrozba123"; try { using (var client = new HttpClient()) { HttpRequestMessage request = new HttpRequestMessage(); request.RequestUri = new Uri(Baseurl); request.Method = HttpMethod.Get; request.Headers.Add("X-Riot-Token", "RGAPI-020de469-e1b9-4bdf-aea1-175739868a0b"); HttpResponseMessage response = await client.SendAsync(request); var responseString = await response.Content.ReadAsStringAsync(); var statusCode = response.StatusCode; if (response.IsSuccessStatusCode) { var responses = JsonConvert.DeserializeObject<LOLSummoner>(responseString); LOLSummoner TestovaciUzivatel = new() { id = responses.id, accountId = responses.accountId, puuid = responses.puuid, name = responses.name, profileIconId = responses.profileIconId, revisionDate = responses.revisionDate, summonerLevel = responses.summonerLevel }; summonername = TestovaciUzivatel.name.ToString(); } else { } } } catch (Exception ex) { throw; } }
错误排查与解决方案
1. 核心问题:数据类型不匹配
Riot API返回的召唤师数据中,profileIconId是整数类型、summonerLevel是长整数类型、revisionDate是时间戳(长整数),但你的模型里全部定义为string类型,反序列化时无法完成类型转换,导致属性值为空。
2. 次要问题:冗余代码与异步方法不当
- 反序列化后的
responses已经是LOLSummoner实例,无需再新建TestovaciUzivatel对象拷贝属性,属于冗余操作。 - 使用
async void定义异步方法会导致异常无法被上层捕获,应改为async Task。
修正后的代码
修正后的模型
using Newtonsoft.Json; // 确保引用此命名空间 public class LOLSummoner { [JsonProperty("id")] public string Id { get; set; } [JsonProperty("accountId")] public string AccountId { get; set; } [JsonProperty("puuid")] public string Puuid { get; set; } [JsonProperty("name")] public string Name { get; set; } [JsonProperty("profileIconId")] public int ProfileIconId { get; set; } // 改为int,匹配API返回类型 [JsonProperty("revisionDate")] public long RevisionDate { get; set; } // 改为long,时间戳类型 [JsonProperty("summonerLevel")] public long SummonerLevel { get; set; } // 改为long,匹配API返回类型 }
修正后的API调用方法
public async Task OnPostCallAPI() { string baseUrl = "https://eun1.api.riotgames.com/lol/summoner/v4/summoners/by-name/PolskaHrozba123"; try { using (var client = new HttpClient()) { var request = new HttpRequestMessage(HttpMethod.Get, baseUrl); request.Headers.Add("X-Riot-Token", "RGAPI-020de469-e1b9-4bdf-aea1-175739868a0b"); var response = await client.SendAsync(request); var responseString = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var summoner = JsonConvert.DeserializeObject<LOLSummoner>(responseString); summonername = summoner?.Name ?? string.Empty; // 直接使用反序列化后的对象 } else { // 可添加错误处理逻辑,比如记录responseString查看错误信息 } } } catch (Exception ex) { // 这里添加异常日志记录,避免直接抛出导致程序崩溃 throw; } }
额外建议
- 调试时输出
responseString,确认API返回的JSON结构是否和模型字段完全匹配,比如是否有拼写错误。 - 可以使用工具先调用API,查看返回的JSON数据,确保模型字段和JSON键名、类型一致。
内容的提问来源于stack exchange,提问作者Drapys
相关产品推荐
相关产品推荐

