我想从dota web api格式获取字符串值是JSON但是没有这样做,url显示的是值,但它没有按我想要的格式化,我需要知道我的代码有什么问题?< / p>
错误:
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);
}
}
}
答案 0 :(得分: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);
}
}