下载并解析Json文件服务器端

时间:2016-01-03 23:38:13

标签: c# json asp.net-mvc-5

我第一次学习如何从第三方供应商处获取json文件,而我正在尝试使用Steam。我正在尝试为特定用户检索游戏名称和特定游戏的游戏时间。基于在线文档,我已经阅读了以下代码应该正常工作,但问题是我得到了返回null。如果我将生成的URL放在浏览器中,我会得到结果,这意味着我的URL很好,但我解析它的方式是错误的。

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";
}

我得到的错误是

  

值不能为空。
  参数名称:source

     

第26行:var json = JsonConvert.DeserializeObject(wc.DownloadString(format));
  第27行:
  第28行:var rocketLeage = json.games.Where(g =&gt; g.appid == rocketLeagueId);
  第29行:var steamGameViewModels = rocketLeage as SteamGameViewModel [] ?? rocketLeage.ToArray();
  第30行:if(steamGameViewModels.Count()== 1)

     

源文件:e:_websites \ Local \ Projects \ Azularis \ Azularis.System.Events \ Azularis.System.Events \ Controllers \ HomeController.cs Line:28

编辑:

我也试过运行代码如下:

var result = wc.DownloadString(format);

var data = JsonConvert.DeserializeObject<SteamMemberViewModel>(result);
var count = data.games.Count();
return count.ToString();

我仍然遇到同样的错误。但结果却带来了价值。

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
            }
        ]
    }
}

SteamMemberViewModel中的值游戏始终为空。

2 个答案:

答案 0 :(得分:1)

我认为你的问题是你的JSON中的顶级对象是响应,但你试图将其解析为相当于游戏数组。我认为你需要获取数组对象,然后解析,所以你需要在一个级别进入JSON对象。

我无法确定,因为您不知道该序列化程序是否正在执行某些递归工作,直到找到该对象(我怀疑它),但文档似乎没有这样做:

http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm

在示例中,它清楚地表明您需要了解您想要成为最高级别的对象。并且在你的例子中没有回应。

所以你必须做一些像(伪代码)的事情:

JObject json = JObject.Parse(jsonString); 
IList<JToken> array = json["response"]["games"].Children().ToList();
if(array != null)
{
    var data = JsonConvert.DeserializeObject<SteamMemberViewModel>(array.ToString());
}

答案 1 :(得分:1)

我知道你已经接受了答案。您的问题很容易解决。用以下代码替换SteamMemberViewModel代码。

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; }
}