c# - Dotnet Core - JSON 值无法转换为 Newtonsoft.Json.Linq.JToken
问题描述
A 有一个 Blazor wasm 应用程序,其定义如下:
{
public int StaticTableId { get; set; }
public JArray StaticData { get; set; }
public static void RunFluent(ModelBuilder modelBuilder)
{
EntityTypeBuilder<StaticTable> entity = modelBuilder.Entity<StaticTable>();
entity.HasKey(p => p.StaticTableId);
entity.Property(e => e.StaticData).HasConversion(
v => v.ToString(),
v => JArray.Parse(string.IsNullOrEmpty(v) ? "[]" : v)
);
}
}
请注意,RunFluent
在DbContext.OnModelCreating
使用控制器中的以下函数直接调用 API 会产生正确的结果;JSON 对象的列表。
// GET: api/StaticTable/5
[HttpGet("{id}")]
public async Task<ActionResult<StaticTable>> StaticTable(int id)
{
var staticTable = await _context.StaticTables.FindAsync(id);
if (staticTable == null)
{
return NotFound();
}
return staticTable;
}
但是,尝试将此数据拉到前端会产生以下错误:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 未处理的异常呈现组件:无法将 JSON 值转换为 Newtonsoft.Json.Linq.JToken。路径:$.projectAnalyses[0].jobInstances[4].resultsData.staticTable.staticData[0] | 行号:0 | BytePositionInLine:3263。`
SO 上的类似问题建议ConfigureServices()
通过调用来更新AddNewtonsoftJson()
,但这已经到位。
更新StaticTable.StaticData
为 typeJToken
也会给我一个错误:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 未处理的异常呈现组件:集合类型“Newtonsoft.Json.Linq.JToken”是抽象的、接口或只读的,无法实例化和填充。
解决方案
推荐阅读
- vba - 发送电子邮件并回复全部给它
- c - 同名指针和正则变量
- python - 来自文件 csv 的错误 insert_one 文档 pymongo
- nopcommerce - Nopcommerce 4.2 自定义插件安装问题
- django - 通过 www 访问。将显示 Nginx 欢迎页面 [+ Gunicorn + Django]
- java - 将价值从 Mojo 传递到 pom
- token - Hyperledger Fabric 2.0,Fab Token 源码在哪里?
- java - 合并多个流加入 Flink
- vue.js - 根据路由在组件中显示来自 Vuex 存储的数据
- php - 我如何将两行中的变量合二为一?