2017-06-12 23 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(); 

你也可以这样做,以提取其他值