C# 如何在Blazor中用API中的json数据填充列表C?
如何填写此列表C# 如何在Blazor中用API中的json数据填充列表C?,c#,json,list,visual-studio,asp.net-core,C#,Json,List,Visual Studio,Asp.net Core,如何填写此列表protectedlist ItemList=new list() 使用blazor中API的json数据 这是Json数据 [ { "id":1, "itemName":"ROUTER MI C3", "idItemBrand":1, "idItemGroup":1, "dateCreated":"2019-12-26T00:00:00", "dateModified":"20
protectedlist ItemList=new list()代码>
使用blazor中API的json数据
这是Json数据
[
{
"id":1,
"itemName":"ROUTER MI C3",
"idItemBrand":1,
"idItemGroup":1,
"dateCreated":"2019-12-26T00:00:00",
"dateModified":"2019-12-26T00:00:00",
"idUserModified":"1"
}
]
这是完整的代码
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Inventory.General;
using Inventory.Model.Master;
using Inventory.Client.Components.Base;
using Microsoft.AspNetCore.Components;
namespace Inventory.Client.Components.Master
{
public class MasterDataBase : InventoryComponentBase
{
protected string error { get; set; }
protected bool TableItem { get; set; } = true;
protected bool TableItemBrand { get; set; } = false;
protected bool TableItemGroup { get; set; } = false;
protected int BtnTable { get; set; } = 0;
protected List<ItemModel> ItemList = new List<ItemModel>();
protected override async Task OnInitializedAsync()
{
GetDataItem();
}
protected async Task GetDataItem()
{
try
{
ItemList = await Http.GetJsonAsync<ItemModel>("api/inventory/v1/item");
}
catch (Exception ex)
{
error = ex.Message;
}
}
}
}
根据json结构,它是对象数组,所以您需要将其解析为集合,而不是单个项:
ItemList = await Http.GetJsonAsync<List<ItemModel>>("api/inventory/v1/item");
ItemList=wait Http.GetJsonAsync(“api/inventory/v1/item”);
我尝试了这个方法,得到了错误“对象引用未设置为对象的实例”。这是否回答了您的问题?不,我认为这和我的情况不同
ItemList = await Http.GetJsonAsync<List<ItemModel>>("api/inventory/v1/item");