我有个问题..如果我使用字符串来显示JSON请求
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“
现在这一切都很好,但问题是如果我使用我的url到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; }
}提前感谢!:)
发布于 2016-04-28 05:31:23
public class RootObject
{
[JsonProperty("585709")]
public List<SummonerId> Summoner_Id { get; set; }
}这解决了我的问题。RootObject是一个导致错误的数字。谢谢,大卫!
https://stackoverflow.com/questions/36877524
复制相似问题