Exception Deserializing

I try to follow these instructions for the .NET SDK:


I may be using newer .NET Core, but it doesn’t compile that way. This compiles:

using Contentful.Core;
using Contentful.Core.Configuration;
using System;
using System.Net.Http;

namespace CfClt
{
class Program
{
public class Entry
{
public string richTextField { get; set; }
}

    static void Main(string[] args)
    {
        HttpClient httpClient = new HttpClient();
        ContentfulOptions options = new ContentfulOptions
        {
            DeliveryApiKey = "Gg3vpa21vheFi28wzcPT7LDy3CgOEb0VJ62cGiPi0JQ",
            PreviewApiKey = "aVrO8Fhmoq-hNEDa78_QHkeqKIxgG6-JhofdnNpCj00",
            SpaceId = "qjiunow8a0ig"
        };

        ContentfulClient client = new ContentfulClient(httpClient, options);
        Entry entry = client.GetEntry<Entry>("4SVaB1ps4H6Ml9ZxWsCWOn").GetAwaiter().GetResult();
        Console.WriteLine(entry.richTextField);
    }
}

}

But I get an exception deserializing from JSON to the POCO. Here is the JSON for the Entry:
{
“sys”: {
“space”: {
“sys”: {
“type”: “Link”,
“linkType”: “Space”,
“id”: “qjiunow8a0ig”
}
},
“id”: “4SVaB1ps4H6Ml9ZxWsCWOn”,
“type”: “Entry”,
“createdAt”: “2020-07-08T19:52:47.34Z”,
“updatedAt”: “2020-07-08T19:52:47.34Z”,
“environment”: {
“sys”: {
“id”: “master”,
“type”: “Link”,
“linkType”: “Environment”
}
},
“revision”: 1,
“contentType”: {
“sys”: {
“type”: “Link”,
“linkType”: “ContentType”,
“id”: “firstContentType”
}
},
“locale”: “en-US”
},
“fields”: {
“richTextField”: {
“nodeType”: “document”,
“data”: {},
“content”: [
{
“nodeType”: “paragraph”,
“content”: [
{
“nodeType”: “text”,
“value”: “this is the rich text field. What is my ID?”,
“marks”: [],
“data”: {}
}
],
“data”: {}
}
]
}
}
}
Here is the exception:
Unhandled exception. Newtonsoft.Json.JsonReaderException: Error reading string. Unexpected token: StartObject. Path ‘fields.richTextField’, line 32, position 22.
at Newtonsoft.Json.JsonReader.ReadAsString()
at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer)
at Newtonsoft.Json.Linq.JToken.ToObject[T](JsonSerializer jsonSerializer)
at Contentful.Core.ContentfulClient.GetEntry[T](String entryId, String queryString, CancellationToken cancellationToken) in C:\temp\cfclt\CfClt\Contentful.Core\ContentfulClient.cs:line 136
at CfClt.Program.Main(String[] args) in c:\temp\cfclt\CfClt\CfClt\Program.cs:line 26

1 Like

I ran into the same error. I think this happens when you attempt to use a string property for a rich text field in your model. Switching to Document, as described here - Getting Started with Contentful rich text and .NET | Contentful - fixed it for me. Hope this helps someone.