简体   繁体   中英

getting something from yahoo weather API

I am trying to create a simple app to get a City name and display the weather with yahoo weather API. I am able to make a json request and get the answer but I am completely lost on how to retrieve the information from this json. I can see the response.query , but when I am trying response.query.something I get undefined. Can someone explain to me how I can get the response.query.results.city thing?

Thanks in advance!!

https://query.yahooapis.com/v1/public/yql?q=select%20 *%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22greenland%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

{
  "query": {
    "count": 1,
    "created": "2015-11-23T16:18:30Z",
    "lang": "vi",
    "results": {
      "channel": {
        "title": "Yahoo! Weather - Greenland, GL",
        "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Greenland__GL/*http://weather.yahoo.com/forecast/GLXX0012_f.html",
        "description": "Yahoo! Weather for Greenland, GL",
        "language": "en-us",
        "lastBuildDate": "Mon, 23 Nov 2015 12:50 pm CGT",
        "ttl": "60",
        "location": {
          "city": "Greenland",
          "country": "Greenland",
          "region": ""
        },
        "units": {
          "distance": "mi",
          "pressure": "in",
          "speed": "mph",
          "temperature": "F"
        },
        "wind": {
          "chill": "15",
          "direction": "220",
          "speed": "15"
        },
        "atmosphere": {
          "humidity": "63",
          "pressure": "29.8",
          "rising": "0",
          "visibility": "6.21"
        },
        "astronomy": {
          "sunrise": "10:45 am",
          "sunset": "1:33 pm"
        },
        "image": {
          "title": "Yahoo! Weather",
          "width": "142",
          "height": "18",
          "link": "http://weather.yahoo.com",
          "url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
        },
        "item": {
          "title": "Conditions for Greenland, GL at 12:50 pm CGT",
          "lat": "71.8",
          "long": "-42.18",
          "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Greenland__GL/*http://weather.yahoo.com/forecast/GLXX0012_f.html",
          "pubDate": "Mon, 23 Nov 2015 12:50 pm CGT",
          "condition": {
            "code": "28",
            "date": "Mon, 23 Nov 2015 12:50 pm CGT",
            "temp": "27",
            "text": "Mostly Cloudy"
          },
          "description": "\n<img src=\"http://l.yimg.com/a/i/us/we/52/28.gif\"/><br />\n<b>Current Conditions:</b><br />\nMostly Cloudy, 27 F<BR />\n<BR /><b>Forecast:</b><BR />\nMon - AM Clouds/PM Sun. High: 29 Low: 17<br />\nTue - Mostly Cloudy. High: 19 Low: 8<br />\nWed - PM Snow Showers. High: 12 Low: 6<br />\nThu - Mostly Cloudy. High: 10 Low: 0<br />\nFri - Mostly Sunny. High: 1 Low: -8<br />\n<br />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Greenland__GL/*http://weather.yahoo.com/forecast/GLXX0012_f.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n",
          "forecast": [
            {
              "code": "30",
              "date": "23 Nov 2015",
              "day": "Mon",
              "high": "29",
              "low": "17",
              "text": "AM Clouds/PM Sun"
            },
            {
              "code": "28",
              "date": "24 Nov 2015",
              "day": "Tue",
              "high": "19",
              "low": "8",
              "text": "Mostly Cloudy"
            },
            {
              "code": "14",
              "date": "25 Nov 2015",
              "day": "Wed",
              "high": "12",
              "low": "6",
              "text": "PM Snow Showers"
            },
            {
              "code": "28",
              "date": "26 Nov 2015",
              "day": "Thu",
              "high": "10",
              "low": "0",
              "text": "Mostly Cloudy"
            },
            {
              "code": "34",
              "date": "27 Nov 2015",
              "day": "Fri",
              "high": "1",
              "low": "-8",
              "text": "Mostly Sunny"
            }
          ],
          "guid": {
            "isPermaLink": "false",
            "content": "GLXX0012_2015_11_27_7_00_CGT"
          }
        }
      }
    }
  }
}

The response you get is a String , not an Object , so you have to Parse it into an Object using JSON.parse(response) .

I think you can try PostMan and JSONView , it will help you to test any API easier.

Hope this can help you.

Just found out what I have to do ( hated js list ). I used dot notation to get my temps. So just for the record

var response; // here i have the jsonparse thing
//I want to access something I have to do 
response.query.results.channel.wind.speed //and i will get wind's speed

If anyone could use the [] notation it would be great. Thanks for your time .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM