未处理的异常 Json 在 Blazor 上显示数据
Posted
技术标签:
【中文标题】未处理的异常 Json 在 Blazor 上显示数据【英文标题】:Unhandled exception Json Display data on Blazor 【发布时间】: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 第一行并在数据周围添加 []。
有什么建议吗?
【问题讨论】:
【参考方案1】:这真的是你的 JSON。数组(或列表)应如下所示
[
"Key": "Are you alive?",
"Value": "true"
,
"Key": "can you tell me what ...?",
"Value": "false"
]
【讨论】:
其实是一个xml文件一开始就转成json了,我试过这个解决方案,其实还不错,但是要应用更改,我不得不重置chrome,谢谢回复和帮助;)以上是关于未处理的异常 Json 在 Blazor 上显示数据的主要内容,如果未能解决你的问题,请参考以下文章
Blazor WASM 异常导致数百个额外的套接字和连接异常
Blazor Swagger:无法加载 API 定义。获取错误:未定义 /swagger/v1/swagger.json
Blazor Webassembly 如何在页面上显示纯文本 (Loader.io)
颤动的json未处理的异常:类型'_InternalLinkedHashMap