1

I am using 'testrpc' and 'truffle' for development.

The following is the contract code used.

pragma solidity ^0.4.0;
import "./usingOraclize.sol";

contract WeatherApiCall is usingOraclize {

           function WeatherApiCall() {
              OAR = OraclizeAddrResolverI(0x5049063e4a7704ac155e4f1f42a4954bbef5bbde);
           }

           function __callback(bytes32 myid, string result) {
                if (msg.sender != oraclize_cbAddress()) throw;

                }

           function update() payable  {
                  oraclize_query("URL", "json(http://api.openweathermap.org/data/2.5/forecast?q='india'&mode=json&APPID=d2e8279188c8649c17540f798c9cc972).list.0.weather.0.main");
           }
 }

The json structure of the url is as follows :-

    {
            "city": {
                  "id": 1273840,
                  "country": "IN" 
            },
            "list": [
                     {
                      "dt": 1486360800,
                       "weather": [
                                     {
                                       "id": 500,
                                       "main": "Rain",
                                       "description": "light rain"
                                      }
                                  ],
                                  "dt_txt": "2017-02-06 06:00:00"
                        },
                        {
                          "dt": 1486371600,
                           "weather": [
                                       {
                                        "id": 801,
                                        "main": "Clouds",
                                        "description": "few clouds"
                                      }
                                     ],
                                       "dt_txt": "2017-02-06 09:00:00"
                                     }
                          ]
           }

The given url "json(http://api.openweathermap.org/data/2.5/forecast?q='india'&mode=json&APPID=d2e8279188c8649c17540f798c9cc972).list.0.weather.0.main" returns the result as "Rain".

I have to retrieve the result as "Rain" or "Clouds" based on the date input as specified by the user?How can we fetch the weather condition based on user input?

0

1 Answer 1

1

The Oraclizequery in solidity is to be updated as follows :-

 oraclize_query("URL", "json(http://api.openweathermap.org/data/2.5/forecast?q='india'&mode=json&APPID=d2e8279188c8649c17540f798c9cc972).list[?(@.dt_txt='2017-02-08 12:00:00')].weather[0].main");

It will return the result as required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.