2016-04-26 65 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是一个导致错误的数字。 谢谢大卫!