2013-06-25 11 views
1

我一直在關注Mike Brinds指南,以在網頁中使用JSON剃鬚刀。在Razor網頁中使用JSON Web服務

唯一的區別是,他的示例使用「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

上面加入JSON – beauXjames

+0

JSON響應提供返回值的副本。謝謝 – Gavin5511

回答

1

您正在尋找在錯誤的地方的價值。 temp_f不是外部對象的一部分;它是current_observation對象的一部分。

改變這一行:

<h3>@search.temp_f</h3> 

這樣:

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

非常感謝您的幫助,像夢一樣運作。 – Gavin5511