C# 使用Newtonsoft.Json从Steam Store API获取数据
我正在尝试从Steam Store API获取数据。目标是向它传递一个AppID,然后使用API获取该游戏的数据,并将其存储在游戏对象中。我正在提取数据直到序列化。以下是我使用的代码:C# 使用Newtonsoft.Json从Steam Store API获取数据,c#,rest,json.net,steam,steam-web-api,C#,Rest,Json.net,Steam,Steam Web Api,我正在尝试从Steam Store API获取数据。目标是向它传递一个AppID,然后使用API获取该游戏的数据,并将其存储在游戏对象中。我正在提取数据直到序列化。以下是我使用的代码: namespace GData { public class Rootobject { public Game _9050 { get; set; } } public class Game { public bool success {
namespace GData
{
public class Rootobject
{
public Game _9050 { get; set; }
}
public class Game
{
public bool success { get; set; }
public Data data { get; set; }
}
public class Data
{
public string type { get; set; }
public string name { get; set; }
public int steam_appid { get; set; }
public int required_age { get; set; }
public bool is_free { get; set; }
public string detailed_description { get; set; }
public string about_the_game { get; set; }
public string short_description { get; set; }
public string supported_languages { get; set; }
public string header_image { get; set; }
public string website { get; set; }
public Pc_Requirements pc_requirements { get; set; }
public object[] mac_requirements { get; set; }
public object[] linux_requirements { get; set; }
public string[] developers { get; set; }
public string[] publishers { get; set; }
public Price_Overview price_overview { get; set; }
public int[] packages { get; set; }
public Package_Groups[] package_groups { get; set; }
public Platforms platforms { get; set; }
public Metacritic metacritic { get; set; }
public Category[] categories { get; set; }
public Genre[] genres { get; set; }
public Screenshot[] screenshots { get; set; }
public Recommendations recommendations { get; set; }
public Release_Date release_date { get; set; }
public Support_Info support_info { get; set; }
public string background { get; set; }
}
public class Pc_Requirements
{
public string minimum { get; set; }
}
public class Price_Overview
{
public string currency { get; set; }
public int initial { get; set; }
public int final { get; set; }
public int discount_percent { get; set; }
}
public class Platforms
{
public bool windows { get; set; }
public bool mac { get; set; }
public bool linux { get; set; }
}
public class Metacritic
{
public int score { get; set; }
public string url { get; set; }
}
public class Recommendations
{
public int total { get; set; }
}
public class Release_Date
{
public bool coming_soon { get; set; }
public string date { get; set; }
}
public class Support_Info
{
public string url { get; set; }
public string email { get; set; }
}
public class Package_Groups
{
public string name { get; set; }
public string title { get; set; }
public string description { get; set; }
public string selection_text { get; set; }
public string save_text { get; set; }
public int display_type { get; set; }
public string is_recurring_subscription { get; set; }
public Sub[] subs { get; set; }
}
public class Sub
{
public int packageid { get; set; }
public string percent_savings_text { get; set; }
public int percent_savings { get; set; }
public string option_text { get; set; }
public string option_description { get; set; }
public string can_get_free_license { get; set; }
public bool is_free_license { get; set; }
public int price_in_cents_with_discount { get; set; }
}
public class Category
{
public int id { get; set; }
public string description { get; set; }
}
public class Genre
{
public string id { get; set; }
public string description { get; set; }
}
public class Screenshot
{
public int id { get; set; }
public string path_thumbnail { get; set; }
public string path_full { get; set; }
}
}
获取JSON:
public class GameData
{
public Game GetGameData(int GameId)
{
var url = "https://store.steampowered.com/api/appdetails/?appids=" + GameId.ToString();
//Game GameData = _download_serialized_json_data<Game>(url);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
string jsonString;
using (Stream stream = WebResp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
Game GameData = new Game();
GameData = JsonConvert.DeserializeObject<Game>(jsonString);
return GameData;
}
}
更新:如果我返回数据数据类型,则所有值均为null
public Data GetGameData(int GameId)
{
var url = "https://store.steampowered.com/api/appdetails/?appids=" + GameId.ToString();
//Game GameData = _download_serialized_json_data<Game>(url);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
string jsonString;
using (Stream stream = WebResp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
var dict = JsonConvert.DeserializeObject<Dictionary<string, Data>>(jsonString);
Data gameData = dict[GameId.ToString()];
return gameData;
}
公共数据GetGameData(int GameId)
{
变量url=”https://store.steampowered.com/api/appdetails/?appids=“+GameId.ToString();
//Game GameData=\u下载\u序列化\u json\u数据(url);
HttpWebRequest WebReq=(HttpWebRequest)WebRequest.Create(string.Format(url));
WebReq.Method=“GET”;
HttpWebResponse WebResp=(HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
字符串jsonString;
使用(Stream-Stream=WebResp.GetResponseStream())
{
StreamReader=新的StreamReader(stream,System.Text.Encoding.UTF8);
jsonString=reader.ReadToEnd();
}
var dict=JsonConvert.DeserializeObject(jsonString);
Data gameData=dict[GameId.ToString()];
返回游戏数据;
}
如果我返回游戏数据类型,我会得到相同的错误:
public Game GetGameData(int GameId)
{
var url = "https://store.steampowered.com/api/appdetails/?appids=" + GameId.ToString();
//Game GameData = _download_serialized_json_data<Game>(url);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
string jsonString;
using (Stream stream = WebResp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
var dict = JsonConvert.DeserializeObject<Dictionary<string, Game>>(jsonString);
Game gameData = dict[GameId.ToString()];
return gameData;
}
公共游戏GetGameData(int GameId)
{
变量url=”https://store.steampowered.com/api/appdetails/?appids=“+GameId.ToString();
//Game GameData=\u下载\u序列化\u json\u数据(url);
HttpWebRequest WebReq=(HttpWebRequest)WebRequest.Create(string.Format(url));
WebReq.Method=“GET”;
HttpWebResponse WebResp=(HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
字符串jsonString;
使用(Stream-Stream=WebResp.GetResponseStream())
{
StreamReader=新的StreamReader(stream,System.Text.Encoding.UTF8);
jsonString=reader.ReadToEnd();
}
var dict=JsonConvert.DeserializeObject(jsonString);
Game gameData=dict[GameId.ToString()];
返回游戏数据;
}
在您的代码中,您正试图反序列化到包含成功
和数据
属性的游戏
类中。但是,在示例JSON中,这些属性不在根级别。取而代之的是,它们在一个名为570
的属性的下面一层,我猜是游戏ID:
{
"570": {
"success": true,
"data": {
...
}
}
}
由于反序列化到的类与JSON不匹配,因此将获得默认值(即,success
为false,data
为null)
要解决这个问题,您需要一些东西来包装您的游戏
类。由于游戏ID可能会根据请求而变化,因此不能使用常规类,如Rootobject
。相反,您需要反序列化到字典中,如下所示:
var dict = JsonConvert.DeserializeObject<Dictionary<string, Game>>(jsonString);
下面是一个工作演示:
注意:在您的类中,您可以省略您并不真正需要的成员。在使用不同的游戏ID时,我发现JSON中的pc_需求
、mac_需求
和linux_需求
元素有时可以是一个对象,有时可以是一个对象数组。如果您将类声明为期望一件事,而JSON返回另一件事,那么在反序列化时将出现错误。您可以使用诸如来自的SingleOrArrayConverter
之类的自定义设置来解决此问题。但是如果你真的不需要这些项目,你可以通过在数据类中注释它们来回避这个问题。你能分享你从API中得到的json示例吗?@ChetanRanpariya Done。使用示例jsonAll更新的问题返回的值为空(使用更新的代码更新的问题)。@Brian我不确定您出了什么问题。我用一个链接更新了我的答案,链接到一个正在运行的演示。希望这会有所帮助。还添加了一些关于处理某些项目的信息,这些项目可能是单个对象或数组,这可能会让您大吃一惊。我不确定。你更新的问题提到了一个错误,但你从未说过错误是什么。工作得很顺利。。谢谢我认为问题是公共类Webm{[JsonProperty(“480”)]公共字符串{u 480{get;set;}公共字符串max{get;set;}
var dict = JsonConvert.DeserializeObject<Dictionary<string, Game>>(jsonString);
Game gameData = dict[GameId.ToString()];