user3459555
user3459555

Reputation: 151

C# Json.net Unexpected character encountered while parsing value: [

public class JSON_Browse_Root
{
    public string browse_content { get; set; }
    public List<JSON_Browse_Content> Content { get; set; }
}

public class JSON_Browse_Content
{
    public string link { get; set; }
    public string image { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string rating { get; set; }
    public string genre { get; set; }
}

using (var client = new HttpClient())
{
    using (HttpResponseMessage response = await client.GetAsync("https://myjsonurl.com"))
    using (HttpContent responseData = response.Content)
    {
        var jsonData = await responseData.ReadAsStringAsync();
        var root = JsonConvert.DeserializeObject<JSON_Browse_Root>(jsonData);
    }

}

JSON:

{
    "browse_content": [
        {
            "link": "https:\/\/blah",
            "image": "https:\/\/blahblah.blah.gif",
            "title": "Mr. Southern Hospitality",
            "description": "He got old money",
            "rating": "e",
            "author": "",
            "genre": "Comedy - Original"
        },
        {
            "link": "https:\/\/blah",
            "image": "https:\/\/blahblah.blah.png",
            "title": "Throwverwatch",
            "description": "Based on the competitive overwatch experience",
            "rating": "t",
            "author": "",
            "genre": "Comedy - Parody"
        }

An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in System.Private.CoreLib.ni.dll but was not handled in user code Unexpected character encountered while parsing value: [

I used this JSON for iOS and Android apps and it's worked perfectly. Also, jsonlint.com says the JSON is valid. Not sure why it's erroring out on the [ character.

Upvotes: 0

Views: 6579

Answers (1)

DavidG
DavidG

Reputation: 118937

Assuming your JSON is missing the ending due to a copy/paste error, the actual problem you have is that your model class is wrong. You have a string property called browse_content and the deserialiser is trying to feed the array (i.e. the [...]) part of the JSON into it, hence you get the error about the unexpected character [. So the simple fix is to update your model to this:

public class JSON_Browse_Root
{
    public List<JSON_Browse_Content> browse_content { get; set; }
}

Upvotes: 3

Related Questions