问题描述
我正在尝试使用Steam的Web API接收JSON并使用JSON.Net对其进行解析.我只是在将要接收JSON的URL上硬编码.我在运行时遇到以下错误:
I'm trying to use Steam's Web API to receive JSON and parse it using JSON.Net. I'm simply hard-coding a URL where I know I'll receive JSON. I'm running into the following error when I run it though:
错误指向我的控制器的第22行:
The error points to line 22 of my controller:
这是我的课程:
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; }
}
我的控制器如下:
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);
我已经在浏览器中访问了URL以验证其正确性,并且还使用JSON Lint测试了URL.我不确定自己在做什么错-我检查了其他主题,并检查了它们列出的问题,但没有找到它们.
I've visited the URL in browser to verify it's correct, and I've also tested the URL using JSON Lint. I'm not sure what I'm doing wrong here - I've checked other topics and checked for the problems they've listed but didn't find them.
推荐答案
您已经创建了RootObject
类型,但是您没有在反序列化中使用它.您是否尝试过:
You've created a RootObject
type, but you're not using it in the deserialization. Have you tried:
var root = JsonConvert.DeserializeObject<RootObject>(json);
// access root.response;
这篇关于解析值时遇到意外字符:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!