c# - 未处理的异常 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:\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 第一行并在数据周围添加 []。
有什么建议吗?
解决方案
它真的是你的 JSON。数组(或列表)应该看起来像
[
{
"Key": "Are you alive?",
"Value": "true"
},
{
"Key": "can you tell me what ...?",
"Value": "false"
}
]
推荐阅读
- sql - 为什么我的 LEFT JOIN 不起作用?
- java - .zip 方法中的 Observable 数量是否有限制?
- python - 在 python 数据框中提取许多 URL
- performance - Azure Web App 的性能测试
- asp.net - 处理按钮单击时的 System.ArgumentOutOfRangeException
- python - 我如何创建多个使用 def 函数作为条件而不重复 def 函数的 if 语句
- javascript - Ajax 时不更新 chrome 上的 css
- python - 文本导入期间标题行中的特殊字符
- cloudkit - CloudKit CKShare URL 无处可去
- r - R 预测包 - 加法和乘法 hw() - ETS 函数中的等效项