【发布时间】:2021-10-22 00:10:34
【问题描述】:
我正在尝试在 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:\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)
这是我的脚本:
Razor 页面“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");
}
}
类 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"
}
}
}
我试着做这个:
- 作业作为作业 [] 而不是列表。
- 将 Job.cs 更改为布尔值。
- 在我的 Json 文件中删除 4 第一行并在数据周围添加 []。
有什么建议吗?
【问题讨论】:
标签: c# json asp.net-core parsing blazor