在Razor网页中使用JSON Web服务

问题描述 投票:1回答:1

我一直在关注Mike Brind的指南,在网页剃须刀中使用JSON。

唯一的区别是,他的示例使用“foreach”循环,但我的JSON返回仅返回单个值参数。下面的代码没有错误输出,但它没有返回屏幕上的值。

@{
var client = new WebClient();
var json = client.DownloadString("http://api.wunderground.com/api/xxxxxxxx/conditions/q/FL/Orlando.json");
var search = Json.Decode(json);
}

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>

<h2>The current temperature is </h2>
<h3>@search.temp_f</h3>


</body>
</html>

这是JSON响应:

{
    "response": {
        "version": "0.1",
        "termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
        "features": {
            "conditions": 1
        }
    },
    "current_observation": {
        "image": {
            "url":"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
            "title":"Weather Underground",
            "link":"http://www.wunderground.com"
        },
        "display_location": {
            "full":"Orlando, FL",
            "city":"Orlando",
            "state":"FL",
            "state_name":"Florida",
            "country":"US",
            "country_iso3166":"US",
            "zip":"32801",
            "latitude":"28.54150772",
            "longitude":"-81.37413788",
            "elevation":"35.00000000"
        },
        "observation_location": {
            "full":"WFTV, Channel 9, Orlando, Florida",
            "city":"WFTV, Channel 9, Orlando",
            "state":"Florida",
            "country":"US",
            "country_iso3166":"US",
            "latitude":"28.537670",
            "longitude":"-81.372108",
            "elevation":"92 ft"
        },
        "estimated": {
        },
        "station_id":"KFLORLAN65",
        "observation_time":"Last Updated on June 25, 2:02 PM EDT",
        "observation_time_rfc822":"Tue, 25 Jun 2013 14:02:02 -0400",
        "observation_epoch":"1372183322",
        "local_time_rfc822":"Tue, 25 Jun 2013 14:07:54 -0400",
        "local_epoch":"1372183674",
        "local_tz_short":"EDT",
        "local_tz_long":"America/New_York",
        "local_tz_offset":"-0400",
        "weather":"Light Rain",
        "temperature_string":"88.7 F (31.5 C)",
        "temp_f":88.7,
        "temp_c":31.5,
        "relative_humidity":"64%",
        "wind_string":"From the East at 7.0 MPH",
        "wind_dir":"East",
        "wind_degrees":101,
        "wind_mph":7.0,
        "wind_gust_mph":0,
        "wind_kph":11.3,
        "wind_gust_kph":0,
        "pressure_mb":"1021",
        "pressure_in":"30.14",
        "pressure_trend":"-",
        "dewpoint_string":"75 F (24 C)",
        "dewpoint_f":75,
        "dewpoint_c":24,
        "heat_index_string":"99 F (37 C)",
        "heat_index_f":99,
        "heat_index_c":37,
        "windchill_string":"NA",
        "windchill_f":"NA",
        "windchill_c":"NA",
        "feelslike_string":"99 F (31.5 C)",
        "feelslike_f":"99",
        "feelslike_c":"31.5",
        "visibility_mi":"10.0",
        "visibility_km":"16.1",
        "solarradiation":"",
        "UV":"8",
        "precip_1hr_string":"-999.00 in ( 0 mm)",
        "precip_1hr_in":"-999.00",
        "precip_1hr_metric":" 0",
        "precip_today_string":"0.00 in (0 mm)",
        "precip_today_in":"0.00",
        "precip_today_metric":"0",
        "icon":"rain",
        "icon_url":"http://icons-ak.wxug.com/i/c/k/rain.gif",
            "forecast_url":"http://www.wunderground.com/US/FL/Orlando.html",
        "history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KFLORLAN65",
        "ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=28.537670,-81.372108"
    }
}
json razor webmatrix
1个回答
1
投票

你在错误的地方寻找价值。 temp_f不是外部物体的一部分;它是current_observation对象的一部分。

改变这一行:

<h3>@search.temp_f</h3>

对此:

<h3>@search.current_observation.temp_f</h3>
相关问题

如何使用下面的类从web服务中动态读取下面给出的JSON数据,并为收到的数据创建水平列表视图。

如何在 Scatch 的 linux ubuntu22.04 中的 vscode 中设置 java web 开发?

将客户端库添加到 Razor 类库

如何拆分 Rails5 web 和 actioncable 服务器 [使它们作为单独的进程运行]

Apache httpd 请求的延迟问题

如何将文件从一个 Web 应用程序复制到另一个虚拟目录

Actix 在 Javascript 文件上托管 React App 400

PM2 进程从 Ubuntu 中消失,没有服务器重启

应用程序负载均衡器无法通过网络模式桥接和动态端口映射对 HTTP AWS ECS 服务进行健康检查

toString() 的最佳实践 - 覆盖 toString() 时性能/内存消耗的任何缺陷

使用 .NET 中的 Soap Web 服务:无法解析签名 URI“#MsgBody”来计算隐式值

get item分页时如何返回ListQueryResult?

列出在 Terraform 中使用的 AWS Billing Filter 选项或名称

Error (#2) 发生内部服务器错误。 Web 服务器正在处理您的请求时发生上述错误 [关闭]

如何为负载均衡器设置静态私有 IP 地址。 Kubernetes/AWS

在IIS上配置soap:address位置到域名

Getting {"errorMessage": "'httpMethod'", "errorType": "KeyError" while testing Aws Lambda function

ASP.Net 控制器到服务连接错误

无法通过 PowerShell 连接到 RDS 服务器

Blazor 服务器应用程序调用 Web Api,均使用 Windows 身份验证,失败并出现 401 错误

热门问答
最新问题