C# 在服务器端下载并解析Json文件
我第一次学习如何从第三方供应商处获取json文件,我正在尝试使用steam实现这一点。我试图为特定用户检索特定游戏的游戏名称和游戏时间。根据我阅读的在线文档,下面的代码应该可以工作,但问题是我得到的是空值。如果我把生成的URL放到浏览器中,我会得到结果,这意味着我的URL是好的,但我解析它的方式是错误的C# 在服务器端下载并解析Json文件,c#,json,asp.net-mvc-5,C#,Json,Asp.net Mvc 5,我第一次学习如何从第三方供应商处获取json文件,我正在尝试使用steam实现这一点。我试图为特定用户检索特定游戏的游戏名称和游戏时间。根据我阅读的在线文档,下面的代码应该可以工作,但问题是我得到的是空值。如果我把生成的URL放到浏览器中,我会得到结果,这意味着我的URL是好的,但我解析它的方式是错误的 public class SteamMemberViewModel { public List<SteamGameViewModel> games { get; set; }
public class SteamMemberViewModel
{
public List<SteamGameViewModel> games { get; set; }
}
public class SteamGameViewModel
{
public int appid { get; set; }
public string name { get; set; }
public int playtime_forever { get; set; }
}
private string GetSteamGame()
{
const int rocketLeagueId = 252950;
var format = string.Format("http://api.steampowered.com/{0}/{1}/v{2}/?key={3}&steamid={4}&include_appinfo=1&format=json", "IPlayerService", "GetOwnedGames", "0001", "ABC", "123");
using (WebClient wc = new WebClient())
{
var json = JsonConvert.DeserializeObject<SteamMemberViewModel>(wc.DownloadString(format));
var rocketLeage = json.games.Where(g => g.appid == rocketLeagueId);
var steamGameViewModels = rocketLeage as SteamGameViewModel[] ?? rocketLeage.ToArray();
if (steamGameViewModels.Count() == 1)
{
var playtime = steamGameViewModels.First().playtime_forever;
return steamGameViewModels.First().name + " - " + playtime;
}
}
return "Steam Game Not Found";
}
SteamMemberViewModel中的值games始终为null。我认为您的问题在于JSON中的顶级对象是响应,但您正试图将其解析为与games数组等效的对象。我认为您需要获取数组对象,然后进行解析,因此需要深入到JSON对象 我不能确定,因为在找到对象之前,您不知道序列化程序是否正在执行一些递归工作(尽管我对此表示怀疑),但文档似乎没有这样做: 在这个例子中,它清楚地说明了你需要深入到你想要成为你的顶层的对象。在你的例子中,这不是回应 因此,您必须执行以下操作(伪代码):
JObject json=JObject.Parse(jsonString);
IList array=json[“response”][“games”].Children().ToList();
if(数组!=null)
{
var data=JsonConvert.DeserializeObject(array.ToString());
}
我知道你已经有了一个公认的答案。你的问题很容易解决。将streammemberviewmodel
代码替换为以下代码
public class SteamMemberViewModel
{
public Response response { get; set; }
}
public class Response
{
public int game_count { get; set; }
public Game[] games { get; set; }
}
public class Game
{
public int appid { get; set; }
public string name { get; set; }
public int playtime_forever { get; set; }
public string img_icon_url { get; set; }
public string img_logo_url { get; set; }
public bool has_community_visible_stats { get; set; }
}
你试过把你的陈述分解成更容易理解的部分吗?可能下载json字符串并将其存储在字符串变量中。然后将字符串变量传递给反序列化对象。这样你就可以添加一个断点,看看你接收的json对象是什么样子。@itsme86是的,我更新了我的答案。我仍然会犯同样的错误。下载结果并从steam检索值,但它无法将其转换为对象?它只有这3个值?@itsme86我用json文件example更新了这个问题,你的例子的结果是什么?我不使用json解析器,所以我只是假设我使用的是这个问题。我没有试过你的例子。上面的这些对你来说还不起作用吗?你是对的,这就是问题所在,我现在正试图找出如何正确地解析它。
{
"response": {
"game_count": 16,
"games": [
{
"appid": 10,
"name": "Counter-Strike",
"playtime_forever": 5019,
"img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5",
"img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f",
"has_community_visible_stats": true
}
]
}
}
JObject json = JObject.Parse(jsonString);
IList<JToken> array = json["response"]["games"].Children().ToList();
if(array != null)
{
var data = JsonConvert.DeserializeObject<SteamMemberViewModel>(array.ToString());
}
public class SteamMemberViewModel
{
public Response response { get; set; }
}
public class Response
{
public int game_count { get; set; }
public Game[] games { get; set; }
}
public class Game
{
public int appid { get; set; }
public string name { get; set; }
public int playtime_forever { get; set; }
public string img_icon_url { get; set; }
public string img_logo_url { get; set; }
public bool has_community_visible_stats { get; set; }
}