如何使用Blazor C#传达JSON的对象?

发布于 2025-02-06 16:01:59 字数 626 浏览 1 评论 0原文

这是我的代码响应变量将返回JSON的响应,现在我想根据返回的值添加if语句,但它会给我带来错误。我尝试了很多事情,但我不明白它是如何做到的。有人请在这里帮助我

@code {

    private CountryTracker response;

    protected override async Task OnInitializedAsync()
    {
        response = await HttpClient.GetFromJsonAsync<CountryTracker>("http://ip-api.com/json/");
    }

    if(response?.country=="Nepal"){
          BlazorTimer timer = new(1000, 35);
        private int secondsLeft = 35;
    }
    else{
          BlazorTimer timer = new(1000, 15);
        private int secondsLeft = 15;
    }

,我想根据JSON的回应执行不同的动作,我该如何做这个plese帮助我。回应的对象是国家。我想在基本国家 /地区实施计时器的操作。

This is my code here response variable will return the response of the json, now I want to add if statement according to the returned values but its throwing me errors. I tried many things but I don't understand how its done. Someone please help me

@code {

    private CountryTracker response;

    protected override async Task OnInitializedAsync()
    {
        response = await HttpClient.GetFromJsonAsync<CountryTracker>("http://ip-api.com/json/");
    }

    if(response?.country=="Nepal"){
          BlazorTimer timer = new(1000, 35);
        private int secondsLeft = 35;
    }
    else{
          BlazorTimer timer = new(1000, 15);
        private int secondsLeft = 15;
    }

Here I want to perform different action according to the json response, how can I do this plese help me. The object of response is country. I want to implement the operation of timer on the basic of country.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟─花易冷 2025-02-13 16:01:59

您不能在代码的顶层写语句。您的逻辑必须采用方法。实例成员(秒缩)不能在语句中有条件地声明。将此与您的代码进行比较:

@code {

    private CountryTracker response;
    private int secondsLeft;

    protected override async Task OnInitializedAsync()
    {
        response = await HttpClient.GetFromJsonAsync<CountryTracker>("http://ip-api.com/json/");

        if(response?.country=="Nepal"){
              BlazorTimer timer = new(1000, 35);
              secondsLeft = 35;
        }
        else{
              BlazorTimer timer = new(1000, 15);
              secondsLeft = 15;
        }
    }
}

You can't write statements at the top level of the code. Your logic must go in a method. And instance members (secondsLeft) cannot be conditionally declared in statements. Compare this with your code:

@code {

    private CountryTracker response;
    private int secondsLeft;

    protected override async Task OnInitializedAsync()
    {
        response = await HttpClient.GetFromJsonAsync<CountryTracker>("http://ip-api.com/json/");

        if(response?.country=="Nepal"){
              BlazorTimer timer = new(1000, 35);
              secondsLeft = 35;
        }
        else{
              BlazorTimer timer = new(1000, 15);
              secondsLeft = 15;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文