如何使用Blazor C#传达JSON的对象?
这是我的代码响应变量将返回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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在代码的顶层写语句。您的逻辑必须采用方法。实例成员(秒缩)不能在语句中有条件地声明。将此与您的代码进行比较:
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: