Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用api调用修复json数据序列化_Json_.net Core_Blazor - Fatal编程技术网

如何使用api调用修复json数据序列化

如何使用api调用修复json数据序列化,json,.net-core,blazor,Json,.net Core,Blazor,我在从API反序列化json时遇到了一些问题 我也尝试过HTTP客户端和rest sharp @page "/Demographics" @using System @using System.Collections.Generic @using System.Diagnostics @using System.Net.Http.Headers @using System.Threading.Tasks @using Microsoft.AspNetCore.Components @usi

我在从API反序列化json时遇到了一些问题

我也尝试过HTTP客户端和rest sharp

   @page "/Demographics"
@using System
@using System.Collections.Generic
@using System.Diagnostics
@using System.Net.Http.Headers
@using System.Threading.Tasks
@using Microsoft.AspNetCore.Components
@using Newtonsoft.Json   
@inject HttpClient Http

<h3>Demographics</h3>
      <table class="table table-bordered">
    <thead>
    <tr>
        @foreach (var c in dyn.cols)
        {
            <td>@c.Value</td>

        }
    </tr>
    </thead>
    <tbody>
    @foreach (var r in dyn.rows)
    {
        <tr>
            @foreach (var d in r)
            {
                <td>@d.Value</td>

            }
        </tr>
    }
    </tbody>

</table>
@code
{
    dynamic dyn;
    //Patient[] patients;

    protected async Task OnInitAsync()
    {

        //patients = await Http.GetJsonAsync<Patient[]>("https://tsrvcis/cis-api/get-patient/by-mrn?format=json");

        var url = "https://tsrvcis/telemedicine-use-case";
        Http.DefaultRequestHeaders.Accept.Clear();
        Http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        Http.DefaultRequestHeaders.Add("Authorization", "042a9ff198c04caf99161406f46eaf21|API Testing Script");
        var response = await Http.GetStringAsync(url);

        dyn = JsonConvert.DeserializeObject(response);
   }
}
@page”/Demographics
@使用系统
@使用System.Collections.Generic
@使用系统诊断
@使用System.Net.Http.Header
@使用System.Threading.Tasks
@使用Microsoft.AspNetCore.Components
@使用Newtonsoft.Json
@注入HttpClient Http
人口统计学的
@foreach(以动态列为单位的变量c)
{
@c、 价值观
}
@foreach(动态行中的变量r)
{
@foreach(r中的变量d)
{
@d、 价值观
}
}
@代码
{
动态dyn;
//病人[]名病人;
受保护的异步任务OnInitAsync()
{
//患者=等待Http.GetJsonAsync(“https://tsrvcis/cis-api/get-patient/by-mrn?format=json");
变量url=”https://tsrvcis/telemedicine-use-case";
Http.DefaultRequestHeaders.Accept.Clear();
Http.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
Http.DefaultRequestHeaders.Add(“授权”,“042a9ff198c04caf99161406f46eaf21 | API测试脚本”);
var response=wait Http.GetStringAsync(url);
dyn=JsonConvert.DeserializeObject(响应);
}
}
我得到的错误是:

预期结果:
应该有动态http表或网格。如果有UI插件或任何其他解决方案,那将非常棒。

从以下内容开始,尝试一下,如果有新的错误,请带着新的错误来到这里:

<h3>Demographics</h3>

@if (dyn == null)
{
   <p>Loading...</p>
} 
else
{ 
      <table class="table table-bordered">
      ....

    </table>
}
人口统计 @如果(dyn==null) { 加载

} 其他的 { .... } 评论:

  • 在使用变量dyn之前,必须确保该变量已填充。

  • 我不确定我是否理解您为什么使用动态类型

  • url是否正确:
  • 这是一个承载令牌权限:“042a9ff198c04caf99161406f46eaf21 | API测试脚本”

  • 您是否知道您使用的HttpClient服务不是实际的或真实的HttpClient

  • Blazor正在使用System.Text.Json。。。那么为什么要使用Newtonsoft.Json呢


    • 我认为您应该在开始时进行一些初始化,如

      dynamic dyn = new {attr1 = new Object(), ...}
      
      有一段时间没有写C#代码(请修复),但正如我所记得的,我通过初始化对象解决了这个错误。
      另外,他使用newtonsoft是因为

      使用HtpClient时出了什么问题?出现了同样的错误。