我需要访问温度并将其附加到我的页面如果这些都是对象,那么我想我不能使用索引,因为顺序无关紧要,这让我失望。
{
"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": "Toms River, NJ",
"city": "Toms River"
},
"observation_location": {
"full": "Stonehedge, Toms River, New Jersey",
"city": "Stonehedge, Toms River",
"state": "New Jersey",
"country": "US"
},
"estimated": {},
"station_id": "KNJTOMSR5",
"observation_time": "Last Updated on April 11, 10:56 PM EDT",
"observation_time_rfc822": "Fri, 11 Apr 2014 22:56:39 -0400",
"observation_epoch": "1397271399",
"local_time_rfc822": "Fri, 11 Apr 2014 22:56:40 -0400",
"local_epoch": "1397271400",
"local_tz_short": "EDT",
"local_tz_long": "America/New_York",
"local_tz_offset": "-0400",
"weather": "Clear",
**"temperature_string": "59.6 F (15.3 C)"**
}
}
我的javascript现在显然是因为我还没弄明白如何访问这个项目
$(function() {
$("#getzip").submit(function() {
var zip_data =$(this).serialize();
$.getJSON("get_weather.php",null, function(data); {
我需要将它附加到我的页面的dom,我认为它应该看起来像这样吗?
(#output).append(data.temperature_string);
答案 0 :(得分:1)
将json对象转换为javascript。
像这样使用。
在Php中
$json ='{"temperature":"36 c"}';
在Javascript中
$.post("get_weather.php",null, function(data){
var temperature = data.temperature; // here you'll get temperature from json into variable
console.log(temperature);
},"json");
全部
答案 1 :(得分:0)
console.log(data [" temperature_string"])应该访问该数据。