Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Using JSONObject to get data from a weather api

Discussion in 'Scripting' started by harriethw, Jun 30, 2017.

  1. harriethw

    harriethw

    Joined:
    Jun 21, 2017
    Posts:
    1
    Hi all,

    I usually find an answer on here or by googling, but am stumped - it does not help that I have never used JSON before. I'm trying to bring in one value from https://openweathermap.org/api and use it to determine what Sprite is shown:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SkyIoT : MonoBehaviour {
    6.  
    7. //the sprites
    8.     public Sprite img1 , img2;
    9.  
    10.  
    11.     IEnumerator AdjustSkyToWeather() {
    12.         while (true) {
    13. //the particular URL from where I want to get this data
    14.             string weatherUrl = "http://api.openweathermap.org/data/2.5/forecast?id=6167863&appid=391013b34d8368540317a2dd0dd2536d";
    15.  
    16.             WWW weatherWWW = new WWW (weatherUrl);
    17.             yield return weatherWWW;
    18.  
    19.             JSONObject tempData = new JSONObject (weatherWWW.text);
    20. //get the data as a JSON Object and then get the first value of Main
    21.             JSONObject weatherDetails = tempData["weather"];
    22.             string WeatherType = weatherDetails[0]["main"].str;
    23.  
    24. //determining which sprite to use
    25.             if (WeatherType == "Clear") {
    26.                 gameObject.GetComponent<SpriteRenderer>().sprite = img1;
    27.                 Debug.Log ("Clear");
    28.             } else if (WeatherType == "Clouds" || WeatherType == "Rain" || WeatherType == "Snow") {
    29.                 gameObject.GetComponent<SpriteRenderer>().sprite = img2;
    30.                 Debug.Log ("Cloudy");
    31.             }
    32. //checking for new data ever ten minutes
    33.             yield return new WaitForSeconds(600);
    34.         }
    35.     }
    36.  
    37.     void Start () {
    38.         StartCoroutine (AdjustSkyToWeather());
    39.     }
    40. }
    41.  
    But I'm getting this error:
    "
    NullReferenceException: Object reference not set to an instance of an object
    SkyIoT+<AdjustSkyToWeather>c__Iterator0.MoveNext () (at Assets/SkyIoT.cs:21)
    UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
    "

    Which refers to this line of code:

    Code (csharp):
    1. string WeatherType = weatherDetails[0] ["main"].str;
    As far as I understand it, that should get the first value that comes after "main"?

    I modified the original code from this site: https://www.sitepoint.com/web-apis-and-iot-in-unity/

    Any help would be greatly appreciated!