Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Text.Json.JsonException: The JSON value could not be converted

I'm using Ubuntu and dotnet 3.1, running vscode's c# extension.

I need to create a List from a JSON file, my controller will do some calculations with this model List that I will pass to it

I followed [these docs][1] examples

So, here is my code and the error I'm getting

First, I thought my error was because at model my attributes were char and C#, for what I saw, cannot interpret double-quotes for char, it should be single quotes. Before losing time removing it, I just changed my type declarations to strings and it's the same error.

Can someone help me?

ElevadorModel

using System.Collections.Generic;

namespace Bla
{
    public class ElevadorModel
    {
        public int andar { get; set; }
        public string elevador { get; set; }
        public string turno { get; set; }
    }

}

Program.cs:

class Program
    {
        static void Main(string[] args)
        {

            var path = "../input.json";

            string jsonString;
            
            ElevadorModel elevadoresModel = new ElevadorModel();

            jsonString = File.ReadAllText(path); //GetType().Name = String

            Console.WriteLine(jsonString); //WORKS           

            elevadoresModel = JsonSerializer.Deserialize<ElevadorModel>(jsonString);

        }
like image 787
nanquim Avatar asked Jan 27 '20 22:01

nanquim


4 Answers

Your input json has an array as the base token, whereas you're expecting an object. You need to change your deserialization to an array of objects.

var elevadoresModels = JsonSerializer.Deserialize<List<ElevadorModel>>(jsonString);
elevadoresModel = elavoresModels.First();
like image 127
David L Avatar answered Nov 15 '22 12:11

David L


Your input JSON is an array of models, however you're trying to deserialize it to a single model.

var models = JsonSerializer.Deserialize<List<ElevadorModel>>(jsonString);
like image 20
Evan Trimboli Avatar answered Nov 15 '22 11:11

Evan Trimboli


This is also a problem in Blazor-Client side. For those calling a single object
e.g ClassName = await Http.GetFromJsonAsync<ClassName>($"api/ClassName/{id}");

This will fail to Deserialize. Using the same System.Text.Json it can be done by:

List<ClassName> ListName = await Http.GetFromJsonAsync<List<ClassName>>($"api/ClassName/{id}");

You can use an array or a list. For some reason System.Text.Json, does not give errors and it is successfully able Deserialize.

To access your object, knowing that it is a single object use:

ListName[0].Property

In your case the latter solution is fine but with the path as the input.

like image 2
Xserge Avatar answered Nov 15 '22 10:11

Xserge


In my case, I was pulling the JSON data to deserialize out of an HTTP response body. It looked like this:

var resp = await _client.GetAsync($"{endpoint}");    
var respBody = await resp.Content.ReadAsStringAsync();
var listOfInstances = JsonSerializer.Deserialize<List<modelType>>(respBody);

And the error would show up. Upon further investigation, I found the respBody string had the JSON base object (an array) wrapped in double quotes...something like this:

"[{\"prop\":\"value\"},...]"

So I added

respBody = respBody.Trim('\"');

And the error changed! Now it was pointing to an invalid character '\'. I changed that line to include

respBody = respBody.Trim('\"').Replace("\\", "");

and it began to deserialize perfectly.

For reference:

var resp = await _client.GetAsync($"{endpoint}");    
var respBody = await resp.Content.ReadAsStringAsync();
respBody = respBody.Trim('\"').Replace("\\", "");
var listOfInstances = JsonSerializer.Deserialize<List<modelType>>(respBody);
like image 1
Shad Avatar answered Nov 15 '22 12:11

Shad