【问题标题】:Getting Error in JSON from Web API values从 Web API 值获取 JSON 中的错误
【发布时间】:2015-06-04 17:03:31
【问题描述】:

我想从 dota web api 获取字符串值,格式是 JSON,但未能这样做,url 正在显示值,但它没有按照我想要的格式进行格式化,我需要知道我的代码有什么问题?

错误:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll 
Additional information: Exception has been thrown by the target of an invocation.

JSON 文件

{
    "result": 
    {
        "games":[
        {
            "players":[
            {
                "account_id": 7054833,
                "name": "Директор",
                "hero_id": 62,
                "team": 0
            },
            {
                "account_id": 228767798,
                "name": "Ya.хз",
                "hero_id": 93,
                "team": 1
            }],
            "radiant_team":
            {
                "team_name": "InG.gaming",
                "team_id": 2374581,
                "team_logo": 534020692608135988,
                "complete": false
            },
            "dire_team":
            {
                "team_name": "Night_Players",
                "team_id": 2090745,
                "team_logo": 36365589572601277,
                "complete": false
            },   
            "lobby_id": 24047453607354324,
            "match_id": 1524116173,
            "spectators": 146,
            "league_id": 2091,
            "stream_delay_s": 120,
            "radiant_series_wins": 0,
            "dire_series_wins": 0,
            "series_type": 0,
            "league_tier": 1,
            "scoreboard":
            {
                "duration": 1233.2655029296875,
                "roshan_respawn_timer": 0,
                "radiant": 
                {
                    "score": 11,
                    "tower_state": 1574,
                    "barracks_state": 63,
                    "picks":[
                    {
                        "hero_id": 26
                    },
                    {
                        "hero_id": 62
                    }],
                    "bans":[
                    {
                        "hero_id": 85
                    },
                    {
                        "hero_id": 30
                    }],
                    "players":[
                    {
                        "player_slot": 1,
                        "account_id": 166715230,
                        "hero_id": 21,
                        "kills": 5,
                        "death": 4,
                        "assists": 0,
                        "last_hits": 60,
                        "denies": 5,
                        "gold": 10,
                        "level": 11,
                        "gold_per_min": 355,
                        "xp_per_min": 337,
                        "ultimate_state": 3,
                        "ultimate_cooldown": 0,
                        "item0": 77,
                        "item1": 50,
                        "item2": 164,
                        "item3": 166,
                        "item4": 46,
                        "item5": 0,
                        "respawn_timer": 18,
                        "position_x": -4199.75,
                        "position_y": -6644.25732421875,
                        "net_worth": 6520
                    },
                    {
                         "player_slot": 2,
                         "account_id": 92826288,
                         "hero_id": 54,
                         "kills": 0,
                         "death": 3,
                         "assists": 4,
                         "last_hits": 87,
                         "denies": 17,
                         "gold": 791,
                         "level": 11,
                         "gold_per_min": 359,
                         "xp_per_min": 370,
                         "ultimate_state": 3,
                         "ultimate_cooldown": 0,
                         "item0": 50,
                         "item1": 151,
                         "item2": 7,
                         "item3": 36,
                         "item4": 182,
                         "item5": 0,
                         "respawn_timer": 0,
                         "position_x": -1377.1424560546875,
                         "position_y": -6401.39501953125,
                         "net_worth": 7066
                     }],

C#代码

public partial class LiveLeagues
{
    public class BasePlayer
    {
        [JsonProperty("account_id")]
        public int AccountId { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("hero_id")]
        public int HeroId { get; set; }

        [JsonProperty("team")]
        public int Team { get; set; }
    }
}

public class ViewLiveLeaguePlayers
    {
        public List<LiveLeagues.BasePlayer> Players { get; set; }
    }

主窗体

       private async void FetchLiveLeagueGames()
       {
        using (var client = new HttpClient())
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync("http://api.steampowered.com/IDOTA2Match_570/GetLiveLeagueGames/v1/?key=************");
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                ViewLiveLeaguePlayers data = JsonConvert.DeserializeObject<ViewLiveLeaguePlayers>(responseBody);
                foreach (var players in data.Players)
                {
                    listView1.Items.Add(players.Name);
                }
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("\nException Caught!");
                Console.WriteLine("Message :{0} ", e.Message);
            }
        }
    }

【问题讨论】:

  • 你能看到异常出现在哪一行吗?
  • 它指向 Program.cs "Application.Run(new frmMain());"
  • 请发布完整的堆栈跟踪!
  • 据我所知,您想要解析不是从顶部开始的子树。您想解析结果/游戏中的玩家。我建议在 Games 属性中包含额外的 Result 类
  • 实际上我有一个公共的局部类 LiveLeagues { [JsonProperty("result")] public LiveGames Result { get;放; } } 我需要做什么?

标签: c# json winforms


【解决方案1】:

根据您的 JSON 结构,您需要在 NET 代码中添加额外的层

public class ResponseData
{
    [JsonProperty("result")]
    public LiveLeagues Result{ get; set; }
}
public class LiveLeagues
{
    [JsonProperty("games")]
    public List<ViewLiveLeaguePlayers> Games{ get; set; }
}

这种情况你可以写

var data = JsonConvert.DeserializeObject<ResponseData>(responseBody);
foreach (var game in data.Result.Games)
{
  foreach (var players in game.Players)
  {
    listView1.Items.Add(players.Name);
  }
}

【讨论】:

  • 谢谢!我稍后会尝试并尽快提供反馈。
  • 我在这里又忘记了一层 - 请检查更新后的答案。你也需要一些包含结果的东西
  • 它有效,兄弟!非常感谢,我得到了球员名单:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 2013-03-29
  • 2018-12-21
  • 1970-01-01
相关资源
最近更新 更多