2017-06-12 58 views
1

我打電話給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; } 
    } 
+1

製作一個模型,它有一個List 參與者和反序列化('JsonConvert.DeserializeObject <...>')進入那個。 json.net只填充它在模型中找到的字段,所以任何在模型中沒有字段的json都會被忽略。 – nbokmans

回答

1

根據我的評論,json.net只反序列化它可以在模型中找到的字段。所以只需在模型中放入你想從json中獲取的字段。

例POCO的:

public class GameData { 
    public string gameId {get; set;} 
    public int mapId {get; set;} //just demonstrating that it only fills in the fields you put in the model 
    public List<Participant> participants = new List<Participant>(); 
} 

...

public 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; } 
} 

反序列化,像這樣:

var gameInfo = JsonConvert.DeserializeObject<GameData>(jsonString); 
var participantsList = gameInfo.participants; 
+0

我試圖做到這一點,但它給了我錯誤: Newtonsoft.Json.JsonSerializationException:'無法反序列化當前的JSON對象(例如{「name」:「value」})到類型'System.Collections.Generic。 List'1 [RiotApiCall1.Participant]',因爲該類型需要一個JSON數組(例如[1,2,3])來正確地反序列化。 – FastCow

+0

錯誤表示這不是你想要做的。它是說你仍然試圖反序列化到List 中。顯示你的反序列化代碼。 – nbokmans

+0

它的代碼我張貼在問題 – FastCow

0

如果你在創建一個多個模型沒有問題,然後創建一個基本模型

public class GameData 
{ 
    public string gameId {get; set;} 
    public int mapId {get; set;} 
    public List<Participant> participants 
} 

,並使用

var gameData =JsonConvert.DeserializeObject<GameData>(jsonString); 

如果你不想再創建一個模型,你可以轉換jsonString到Jobject然後提取出來唱歌值或從該值 對於例如反序列化,

var jsonString = GetResponse("https://euw1.api.riotgames.com/lol/spectator/v3/active-games/by-summoner/79200188?api_key=RGAPI-dc7c328a-dd2b-40ca-8ccd-71d05d530a4e"); 

JObject json = JObject.Parse(jsonString); 
var gameId = json .GetValue("filetype").ToString(); 

你也可以這樣做,以提取其他值