user6229886
user6229886

Reputation:

How do I read specific sections of a large JSON string?

I make a call to the Riot Games API https://developer.riotgames.com/ It returns this huge output (link to pastebin) in JSON.

The JSON contains 6 "Participants" and I want to create 6 participant objects from the output. My problem is that there are other sections than the "Participants" such as "gameid", "gamestarttime" and etc. Because of this I don't know how to read out only the JSON text under the "participants:" section.

In short, how do I get single out only the data under "participants" or how do I create only objects from the "participants" section of the output?

Here is my code that would work if there wasn't anything other than participants:

        // 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);
        }

Here is my Participant class which from which I want to create objects from the 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; }
    }

Upvotes: 1

Views: 2599

Answers (2)

Sagar Jumble
Sagar Jumble

Reputation: 1

If you have no problem in creating one more model then create a Base Model

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

and use

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

If you do not want to create one more model the you can convert the jsonString to Jobject then extract out sing values or deserialize from that value For eg,

 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();

You can do this to extract other values too

Upvotes: 0

nbokmans
nbokmans

Reputation: 5747

As per my comment, json.net only deserializes the fields that it can find in the model. So just put only the fields you want to take from the json in the model.

Example POCO's:

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

Deserialize like so:

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

Upvotes: 0

Related Questions