0

I'm trying to display a Json file added to my project on blazor, but I got this following error :

   Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: The JSON value could not be converted to Blazor_fresh_project.Shared.Job[]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
System.Text.Json.JsonException: The JSON value could not be converted to Blazor_fresh_project.Shared.Job[]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
   at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
   at System.Text.Json.Serialization.Converters.IEnumerableDefaultConverter`2[[Blazor_fresh_project.Shared.Job[], Blazor_fresh_project.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Blazor_fresh_project.Shared.Job, Blazor_fresh_project.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, Job[]& value)
   at System.Text.Json.Serialization.JsonConverter`1[[Blazor_fresh_project.Shared.Job[], Blazor_fresh_project.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, Job[]& value)
   at System.Text.Json.Serialization.JsonConverter`1[[Blazor_fresh_project.Shared.Job[], Blazor_fresh_project.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[Job[]](JsonConverter jsonConverter, Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[Job[]](JsonReaderState& readerState, Boolean isFinalBlock, ReadOnlySpan`1 buffer, JsonSerializerOptions options, ReadStack& state, JsonConverter converterBase)
   at System.Text.Json.JsonSerializer.<ReadAsync>d__20`1[[Blazor_fresh_project.Shared.Job[], Blazor_fresh_project.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
   at System.Net.Http.Json.HttpContentJsonExtensions.<ReadFromJsonAsyncCore>d__3`1[[Blazor_fresh_project.Shared.Job[], Blazor_fresh_project.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
   at System.Net.Http.Json.HttpClientJsonExtensions.<GetFromJsonAsyncCore>d__9`1[[Blazor_fresh_project.Shared.Job[], Blazor_fresh_project.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
   at Blazor_fresh_project.Client.Pages.Sugestionpool.OnInitializedAsync() in C:\Users\VFlor\source\repos\Blazor_fresh_project\Blazor_fresh_project\Client\Pages\Sugestionpool.razor:line 40
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

Here is my scripts:

Razor page "Sugestionpool.razor"

@using Blazor_fresh_project.Shared
@page "/AskIt"

@inject HttpClient Http;



<h1>Sugesstion pool</h1>
@if(Jobs==null)
{
    <p><em>Loading....</em></p>
}
else
{

<table class="table">
    <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">Name of job</th>
            <th scope="col">State of job</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            @foreach (Job job in Jobs)
            {
                <td>@job.Key</td>
                <td>@job.Value</td>
            }
        </tr>
    </tbody>
</table>
}
@code {

    private List<Job> Jobs;
    protected override async Task OnInitializedAsync()
    {
        Jobs = await Http.GetFromJsonAsync<List<Job>>("sample-data/job.json");
    }
}

class Job.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Blazor_fresh_project.Shared
{
    public class Job
    {
        [Required]
        public string Value { get; set; }
        [Required]
        public string Key { get; set; }
    }
}

Json file "job.json" :

{
  "ArrayOfDataManager": {
    "-xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
    "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",

      {
        "Key": "Are you alive?",
        "Value": "true"
      },
      {
        "Key": "Is it working?",
        "Value": "true"
      },
      {
        "Key": "Working ?",
        "Value": "false"
      },
      {
        "Key": "can you tell me what are yu doing",
        "Value": "true"
      },
      {
        "Key": "can you tell me what ...?",
        "Value": "false"
      }

  }
}

I tried to make this :

  1. jobs as job[] instead of list.
  2. Change Job.cs : value as bool.
  3. Remove in my Json file 4 first lines and add [] around data.

Any suggestion?

1 Answer 1

1

It really is your JSon. An array (or List) should look like

[
  {
    "Key": "Are you alive?",
    "Value": "true"
  },


  {
    "Key": "can you tell me what ...?",
    "Value": "false"
  }
]
Sign up to request clarification or add additional context in comments.

1 Comment

It was in fact an xml file converted into json at start, I tried this solution, which in fact was the good one, but to apply the changes, I had to reset chrome, thank you for reply and help ;)

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.