我打電話給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