亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JSON 到 ListView 中的 C#

JSON 到 ListView 中的 C#

C#
開滿天機 2023-12-17 21:13:39
我想制作應用程序,其中列出了我在 Steam 上的游戲。我得到了 Steam API 密鑰,我得到了帶有游戲數據的 JSON,但我不知道如何將其發送到我的 ListView 或類似的東西。我試圖找到一些教程,我找到了Newtonsoft.JSON,但我仍然遇到錯誤。JSON 的外觀:{   "response":{      "game_count":27,      "games":[         {            "appid":730,            "name":"Counter-Strike: Global Offensive",            "playtime_2weeks":986,            "playtime_forever":30571,            "img_icon_url":"69f7ebe2735c366c65c0b33dae00e12dc40edbe4",            "img_logo_url":"d0595ff02f5c79fd19b06f4d6165c3fda2372820",            "has_community_visible_stats":true,            "playtime_windows_forever":1600,            "playtime_mac_forever":0,            "playtime_linux_forever":0         },         {            "appid":224260,            "name":"No More Room in Hell",            "playtime_forever":0,            "img_icon_url":"684de0d9c5749b5ddd52f120894fd97efd620b1d",            "img_logo_url":"670e9aba35dc53a6eb2bc686d302d357a4939489",            "has_community_visible_stats":true,            "playtime_windows_forever":0,            "playtime_mac_forever":0,            "playtime_linux_forever":0         },         {            "appid":232010,            "name":"Euro Truck Simulator",            "playtime_forever":11,            "img_icon_url":"6b1bb4a4e2b1e0d85ad0b3e2d6d15a0258aa43a0",            "img_logo_url":"fa3886315d9586671506c7149c1e4ecae653ce13",            "has_community_visible_stats":true,            "playtime_windows_forever":0,            "playtime_mac_forever":0,            "playtime_linux_forever":0         }      ]   }}我嘗試過這個,但它不起作用。json = wc.DownloadString("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=D4718FA8ED43C531523CF79310BE52FE&steamid=76561198440001695&include_appinfo=1");dynamic results = JsonConvert.DeserializeObject(json);var appid = results.appid();我收到錯誤:$exception {“組件 ”Newtonsoft.Json.Linq.JObject“ 不包含定義 ”appid“.”}Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
查看完整描述

1 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

應使用 Newtonsoft 的泛型反序列化器方法反序列化此字符串。


嘗試創建一個新類:


 public partial class GameResponse

{

    [JsonProperty("response")]

    public Response Response { get; set; }

}


public partial class Response

{

    [JsonProperty("game_count")]

    public long GameCount { get; set; }


    [JsonProperty("games")]

    public Game[] Games { get; set; }

}


public partial class Game

{

    [JsonProperty("appid")]

    public long Appid { get; set; }


    [JsonProperty("name")]

    public string Name { get; set; }


    [JsonProperty("playtime_2weeks", NullValueHandling = NullValueHandling.Ignore)]

    public long? Playtime2Weeks { get; set; }


    [JsonProperty("playtime_forever")]

    public long PlaytimeForever { get; set; }


    [JsonProperty("img_icon_url")]

    public string ImgIconUrl { get; set; }


    [JsonProperty("img_logo_url")]

    public string ImgLogoUrl { get; set; }


    [JsonProperty("has_community_visible_stats")]

    public bool HasCommunityVisibleStats { get; set; }


    [JsonProperty("playtime_windows_forever")]

    public long PlaytimeWindowsForever { get; set; }


    [JsonProperty("playtime_mac_forever")]

    public long PlaytimeMacForever { get; set; }


    [JsonProperty("playtime_linux_forever")]

    public long PlaytimeLinuxForever { get; set; }

}

然后使用:


var json = wc.DownloadString("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=D4718FA8ED43C531523CF79310BE52FE&steamid=76561198440001695&include_appinfo=1");

var results = JsonConvert.DeserializeObject<GameResponse>(json);

小提琴:https://dotnetfiddle.net/Nf2LdB


查看完整回答
反對 回復 2023-12-17
  • 1 回答
  • 0 關注
  • 252 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號