Json 分析值时遇到意外字符:

Json 分析值时遇到意外字符:,json,asp.net-mvc,parsing,json.net,webclient,Json,Asp.net Mvc,Parsing,Json.net,Webclient,我正在尝试使用Steam的Web API来接收JSON并使用JSON.Net解析它。我只是在硬编码一个URL,我知道我会收到JSON。但在运行时,我遇到了以下错误: 分析值时遇到意外字符:。路径“”,第行 0,位置0 错误指向控制器的第22行: 第22行:SteamResponse-response=JsonConvert.DeserializeObject(json) 以下是我的课程: public class Game { public int appid { get; set; }

我正在尝试使用Steam的Web API来接收JSON并使用JSON.Net解析它。我只是在硬编码一个URL,我知道我会收到JSON。但在运行时,我遇到了以下错误:

分析值时遇到意外字符:。路径“”,第行 0,位置0

错误指向控制器的第22行:

第22行:SteamResponse-response=JsonConvert.DeserializeObject(json)

以下是我的课程:

public class Game
{
    public int appid { get; set; }
    public int playtime_forever { get; set; }
    public int? playtime_2weeks { get; set; }
}

public class SteamResponse
{
    public int game_count { get; set; }
    public List<Game> games { get; set; }
}

public class RootObject
{
    public SteamResponse response { get; set; }
}
公共类游戏
{
公共int-appid{get;set;}
public int playtime_ever{get;set;}
公共int?游戏时间2周{get;set;}
}
公开课回应
{
公共整数博弈_计数{get;set;}
公共列表游戏{get;set;}
}
公共类根对象
{
公共流响应{get;set;}
}
我的控制器如下所示:

        List<Game> gameList = new List<Game>();

        WebClient wc = new WebClient();

        var json = wc.DownloadString("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=" + steamAPIKey + "&steamid=76561197994394917&format=json");

        SteamResponse response = JsonConvert.DeserializeObject<SteamResponse>(json);
List gameList=newlist();
WebClient wc=新的WebClient();
var json=wc.DownloadString(“http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=“+streamapikey+”&streamid=76561197994394917&format=json”);
StreamResponse=JsonConvert.DeserializeObject(json);
我在浏览器中访问了URL以验证其正确性,并且还使用JSON Lint测试了URL。我不确定我做错了什么-我检查了其他主题,检查了他们列出的问题,但没有找到


.

您已经创建了一个
RootObject
类型,但是您没有在反序列化中使用它。您是否尝试过:

var root = JsonConvert.DeserializeObject<RootObject>(json);
// access root.response;
var root=JsonConvert.DeserializeObject(json);
//访问root.response;

感谢您的快速回复。是的,我尝试了RootObject,但我得到了相同的错误。可能是一个无效的流API键,您提供的JSON链接对我有效-API键位于JSON链接的URL中,所以它必须是正确的,对吗?我甚至将您的代码示例复制到了我刚刚创建并运行的本地控制台应用程序中,但得到了相同的错误。我很困惑。你是否调试过,你得到的是相同的JSON响应?好吧,那么问题显然是我的连接。出于某种原因,webclient不会在我的移动连接上引入JSON——我切换到了另一个连接,一切正常。它正在工作。同样重要的是,我使用了错误的对象—RootObject—来解析,所以感谢您指出这一点。