Jorbyte Jorbyte - 1 year ago 114
JSON Question

Unexpected character encountered while parsing value:

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:

Unexpected character encountered while parsing value: . Path '', line
0, position 0.

The error points to line 22 of my controller:

Line 22: SteamResponse response = JsonConvert.DeserializeObject(json);

Here are my classes:

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("" + steamAPIKey + "&steamid=76561197994394917&format=json");

SteamResponse response = JsonConvert.DeserializeObject<SteamResponse>(json);

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.

Here is a link to the JSON I'm attempting to receive.

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download