首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#安卓列表返回NULL - JSON API

C#安卓列表返回NULL - JSON API
EN

Stack Overflow用户
提问于 2016-04-27 07:23:12
回答 1查看 33关注 0票数 0

我有个问题..如果我使用字符串来显示JSON请求

代码语言:javascript
复制
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
    }]
 }]
 }";  

我可以反序列化..

代码语言:javascript
复制
  var root = JsonConvert.DeserializeObject<RootObject>(json);
  var s = root.Summoner_Id[0].queue.ToString();

其中s返回值"RANKED_SOLO_5x5“

现在这一切都很好,但问题是如果我使用我的url到json..

代码语言:javascript
复制
   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;
            }
        }
    }

然后尝试反序列化..

代码语言:javascript
复制
 var root = JsonConvert.DeserializeObject<RootObject>(json);
        var s = root.Summoner_Id[0].queue.ToString();

它抛出一个错误:

代码语言:javascript
复制
System.NullReferenceException: Object reference not set to an instance of an object

这是我的班级。

代码语言:javascript
复制
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; }
}

提前感谢!:)

EN

回答 1

Stack Overflow用户

发布于 2016-04-28 05:31:23

代码语言:javascript
复制
public class RootObject
{
[JsonProperty("585709")]
public List<SummonerId> Summoner_Id     { get; set; }
}

这解决了我的问题。RootObject是一个导致错误的数字。谢谢,大卫!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36877524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档