c# - Getting Error in JSON from Web API values -
i want string values dota web api format json failed so, url displaying values it's not formatted want, need know what's wrong code?
the error:
an unhandled exception of type 'system.reflection.targetinvocationexception' occurred in mscorlib.dll additional information: exception has been thrown target of invocation.
json file
{ "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# code
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; } }
main form
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); } } }
according json structure need layer in net code
public class responsedata { [jsonproperty("result")] public liveleagues result{ get; set; } } public class liveleagues { [jsonproperty("games")] public list<viewliveleagueplayers> games{ get; set; } }
this case can write
var data = jsonconvert.deserializeobject<responsedata>(responsebody); foreach (var game in data.result.games) { foreach (var players in game.players) { listview1.items.add(players.name); } }
Comments
Post a Comment