2016-04-26 43 views
0

林有問題.. 如果我使用一個字符串來顯示我的JSON請求C#的Android列出返回NULL - JSON API

string json2 = @" { 
""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); 
    var s = root.Summoner_Id[0].queue.ToString(); 

在哪裏s返回值「RANKED_SOLO_5x5」

現在,這是偉大的,但問題是,如果我使用我的網址json ..

string url = "https://oce.api.pvp.net/api/lol/oce/v2.5/league/by-summoner/585709/entry?api_key=" + KEY; 
    JsonValue json = await JSONAsync(url); 


private async Task<JsonValue> JSONAsync(string url) 
    { 
     // Create an HTTP web request using the URL: 
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url)); 
     request.ContentType = "application/json"; 
     request.Method = "GET"; 

     // Send the request to the server and wait for the response: 
     using (WebResponse response = await request.GetResponseAsync()) 
     { 
      // Get a stream representation of the HTTP web response: 
      using (Stream stream = response.GetResponseStream()) 
      { 
       // Use this stream to build a JSON document object: 
       JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream)); 
       Console.Out.WriteLine("Response: {0}", jsonDoc.ToString()); 

       // Return the JSON document: 
       return jsonDoc; 
      } 
     } 
    } 

,然後嘗試反序列化..

var root = JsonConvert.DeserializeObject<RootObject>(json); 
     var s = root.Summoner_Id[0].queue.ToString(); 

它拋出一個錯誤:

System.NullReferenceException: Object reference not set to an instance of an object 

這裏是我的課。

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

預先感謝您! :)

+0

您是否比較過下載的JSON字符串與用於測試的硬編碼字符串?他們是一樣的嗎? – Mikanikal

回答

0
public class RootObject 
{ 
[JsonProperty("585709")] 
public List<SummonerId> Summoner_Id  { get; set; } 
} 

這解決了我的問題。 RootObject是一個導致錯誤的數字。 謝謝大衛!