1

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("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=" + 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.

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the quick reply. Yes, I've tried RootObject and I'm getting the same error.
Perhaps it's an invalid stream API key, the JSON link you provided is working for me - dotnetfiddle.net/2KP7HO
Well the API key is in the URL for the JSON link, so it must be correct right? I even copied your code example into a local console app I just created and ran it - but got the same error. I'm confused.
Have you debugged that you're getting back the same JSON response?
Okay so the problem was apparently my connection. For some reason webclient wouldn't pull in JSON on my mobile connection - I switched to a different connection and it was fine. It's working now. It was also important that I was using the wrong object - RootObject - to parse to, so thanks for pointing that out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.