在Razor网页中使用JSON Web服务

时间:2013-06-25 15:37:29

标签: json razor webmatrix

我一直在关注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"
    }
}

1 个答案:

答案 0 :(得分:1)

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

更改此行:

<h3>@search.temp_f</h3>

到此:

<h3>@search.current_observation.temp_f</h3>