未处理的异常Json在Blazor上显示数据



我试图在blazor上显示添加到我的项目的Json文件,但我得到了以下错误:

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:UsersVFlorsourcereposBlazor_fresh_projectBlazor_fresh_projectClientPagesSugestionpool.razor:line 40
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

下面是我的脚本:

Razor页面" suggested pool. 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");
}
}

类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文件"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"
}
}
}

我试着做这个:

  1. jobs as job[]而不是list.
  2. Change Job.cs: value为bool.
  3. 在我的Json文件中删除第4行,并在data周围添加[]。

任何建议吗?

这真的是你的JSon。数组(或列表)应该看起来像

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

{
"Key": "can you tell me what ...?",
"Value": "false"
}
]

相关内容

  • 没有找到相关文章

最新更新