Error from System.Text.Json trying to deserialize a single string

Dennis Tabako 36 Reputation points
2021-11-18T16:26:23.673+00:00

In my Blazor app, I am getting a response back from the API that consists of a single string value. I'm trying to deserialize using:

return await JsonSerializer.DeserializeAsync<T>(await response.Content.ReadAsStreamAsync(), options);

In the above case, T is string. This is producing a "System.Text.Json.JsonException: 'V' is an invalid start of a value" error (V is the first character of the string being returned from the API). Visually inspecting the response content in the debugger, it looks like a UTF-8 encoded string. Is there something I'm missing?

Thanks!
Dennis

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Blazor
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-11-19T02:23:49.443+00:00

    Your issue is the string is not valid json. A json string has quotes around it. Ex

    “Valid json string”

    So your server is not returning json. The client code should check the content-type. It’s probably text, not json.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.