Json.NET - 意外的字符在解析值时遇到: [英] Json.NET - Unexpected character encountered while parsing value:
问题描述
我试图使用蒸汽的Web API来接收JSON和使用JSON.Net解析它。我只是硬编码的网址,让我知道我会得到JSON。我遇到以下错误,当我运行它,但:
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:
在解析值意外遇到性格。路径',行
0,位置0。
Unexpected character encountered while parsing value: . Path '', line 0, position 0.
错误指向行我控制器22:
The error points to line 22 of my controller:
第22行:SteamResponse响应= JsonConvert.DeserializeObject(JSON);
Line 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; }
}
我的控制器看起来是这样的:
My controller looks like this:
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);
我所访问过的网址在浏览器中,以验证它是正确的,我还测试了使用JSON皮棉的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.
<一个href=\"http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=30A260C0B3B379A218C5533CC14CA96D&steamid=76561197994394917&format=json\"相对=nofollow>这里是我试图获得 JSON的一个环节。
Here is a link to the JSON I'm attempting to receive.
推荐答案
您已经创建了一个 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;
这篇关于Json.NET - 意外的字符在解析值时遇到:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!