2016-04-26 16 views
0
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 genereated haben diese Klasse mit Json2csharp.com.C# JSON Gibt eine Liste Innerhalb Liste

Wo die Klasse hat 1 Liste Ich bin in der Lage, auf die Daten ohne Probleme zuzugreifen.

Aber mit dieser Klasse generiert 2 Listen. Ich glaube, ich bin jetzt vorbei zu denken und haben sehr verwirrt ..

Wie kann ich deserialisieren dieser Klasse

string url = json.ToString(); 

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

Summoner_Id kehrt als null.

var id = root.Summoner_Id; 

Wurzel kehrt als null auch ..

Wie kann ich dieses Problem lösen? Bitte hilf mir oder weise mich in die richtige Richtung!

+1

Ihre JSON zeigen, ist es beginnt und endet mit Klammern [und]? – cerberus

+0

Danke für die schnelle Antwort. {"Summoner_Id": [{"name": "Fioras Inquisitoren", "tier": "GOLD", "queue": "RANKED_SOLO_5x5", "entries": [{"playerOrTeamId": "585709", "playerOrTeamName ":" AP Ezreal Mid "," Division ":" IV "," LigaPunkte ": 61," gewinnt ": 175," Verluste ": 158," isHotStreak ": false," isVeteran ": false," isFreshBlood ": false, "isInactive": false}]}]} – user6256751

+0

Dies ist korrekt formatiert Version Ihrer JSON .... [ { "name": "Fiora des Inquisitors", "tier": "GOLD", "queue": "RANKED_SOLO_5x5" "Einträge": [{ "playerOrTeamId ":" 585.709" "playerOrTeamName": "AP Ezreal Mid" "Division": "IV", "LeaguePoints": 61, "gewinnt": 175, "Verluste": 158, "isHotStreak": fal se, "isVeteran": false, "isFreshBlood": false, "isInactive": false } ] } ] – Pravin

Antwort

1

Dieses Beispiel funktioniert für mich:

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