我打电话给Riot Games API https://developer.riotgames.com/ 它以JSON的形式返回this huge output(链接到pastebin)。如何阅读大型JSON字符串的特定部分?
JSON包含6个“参与者”,我想从输出创建6个参与者对象。我的问题是除了“参与者”之外还有其他部分,比如“gameid”,“gamestarttime”等。因此,我不知道如何只读出“参与者”部分下的JSON文本。
简而言之,我该如何在“参与者”下获取单个数据,或者如何仅从输出的“参与者”部分创建对象?
这里是我的代码,如果没有以外的任何其他参与者,将工作:
// Create a string containing the output from the API call
var jsonString = GetResponse("https://euw1.api.riotgames.com/lol/spectator/v3/active-games/by-summoner/79200188?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxx");
// Create a list of Participants containing the data from the JSON
var participantsList = JsonConvert.DeserializeObject<List<Participant>>(jsonString);
// Show the names of the participants
foreach (var item in participantsList)
{
MessageBox.Show(item.summonerName);
}
这里是从我要创建从JSON对象我参加班。
class Participant
{
public int profileIconId { get; set; }
public int championId { get; set; }
public string summonerName { get; set; }
public bool bot { get; set; }
public int spell2Id { get; set; }
public int teamId { get; set; }
public int spell1Id { get; set; }
public int summonerId { get; set; }
}
制作一个模型,它有一个List参与者和反序列化('JsonConvert.DeserializeObject <...>')进入那个。 json.net只填充它在模型中找到的字段,所以任何在模型中没有字段的json都会被忽略。 –
nbokmans