2016-04-26 51 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; } 
} 

我已經genereated使用Json2csharp.com這個類。C#JSON返回一個列表裏面列出

如果類有1個列表,我可以毫無問題地訪問數據。

但是這個班生成了2個列表。我覺得我已經結束了思考,並已成爲很迷茫..

我如何反序列化這個類

string url = json.ToString(); 

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

Summoner_Id收益爲null

var id = root.Summoner_Id; 

根收益爲null也..

我該如何解決這個問題?請幫助或指引我正確的方向!

+1

顯示你的JSON,是它開始與括號[和]結束? – cerberus

+0

感謝您的快速回復。 {「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,」loss「:158,」isHotStreak「:false,」isVeteran「:false,」isFreshBlood「:假 「ISINACTIVE」:假}]}]} – user6256751

+0

這是您的JSON .... [ { 「名」 的格式正確的版本: 「Fiora的的調查官」, 「梯隊」: 「GOLD」, 「隊列」: 「RANKED_SOLO_5x5」, 「條目」:[{ 「playerOrTeamId 」:「 585709」, 「playerOrTeamName」: 「AP Ezreal中」, 「分化」: 「IV」, 「leaguePoints」:61, 「勝」:175, 「損失」:158, 「isHotStreak」:FAL SE, 「isVeteran」:假, 「isFreshBlood」:假, 「ISINACTIVE」:假 } ] } ] – Pravin

回答

1

這個例子對我的作品:

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