김승진
김승진

Reputation: 25

C# JSON Returns a List Inside List

public class Entry
{
    public string playerOrTeamId { get; set; }
    public string playerOrTeamName { get; set; }
    public string division { get; set; }
    public int leaguePoints { get; set; }
    public int wins { get; set; }
    public int losses { get; set; }
    public bool isHotStreak { get; set; }
    public bool isVeteran { get; set; }
    public bool isFreshBlood { get; set; }
    public bool isInactive { get; set; }
}

public class SummonerId
{
    public string name { get; set; }
    public string tier { get; set; }
    public string queue { get; set; }
    public List<Entry> entries { get; set; }
}

public class RootObject
{
    public List<SummonerId> Summoner_Id { get; set; }
}

I have genereated this class using Json2csharp.com.

Where the class has 1 List I am able to access the data with no problems.

But with this class generated 2 Lists. I think I am over thinking now and have become very confused..

How can I deserialize this class

string url = json.ToString();

var root = JsonConvert.DeserializeObject<RootObject>(url):

Summoner_Id returns as null.

var id = root.Summoner_Id;

root returns as null also..

How can I solve this? Please help or point me in the right direction!

Upvotes: 0

Views: 1439

Answers (1)

MrApnea
MrApnea

Reputation: 1946

This example works for me:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Test {
    static class Program {
        static void Main() {

            string json = @" {
    ""Summoner_Id"": [{
        ""name"": ""Fiora's Inquisitors"",
        ""tier"": ""GOLD"",
        ""queue"": ""RANKED_SOLO_5x5"",
        ""entries"": [{
            ""playerOrTeamId‌​"": ""585709"",
            ""playerOrTeamName"": ""AP Ezreal Mid"",
            ""division"": ""IV"",
            ""leaguePoints"": 61,
            ""wins"": 175,
            ""losses"": 158,
            ""isHotStreak"": false,
            ""isVeteran"": false,
            ""isFreshBlood"": false,
            ""isInactive"": false
        }]
    }]
 }";

            var root = JsonConvert.DeserializeObject<RootObject>(json);
            Console.WriteLine(root.Summoner_Id);
        }
    }

    public class Entry {
        public string playerOrTeamId { get; set; }
        public string playerOrTeamName { get; set; }
        public string division { get; set; }
        public int leaguePoints { get; set; }
        public int wins { get; set; }
        public int losses { get; set; }
        public bool isHotStreak { get; set; }
        public bool isVeteran { get; set; }
        public bool isFreshBlood { get; set; }
        public bool isInactive { get; set; }
    }

    public class SummonerId {
        public string name { get; set; }
        public string tier { get; set; }
        public string queue { get; set; }
        public List<Entry> entries { get; set; }
    }

    public class RootObject {
        public List<SummonerId> Summoner_Id { get; set; }
    }
}

Upvotes: 2

Related Questions