Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Using JSONObject to get data from a weather api

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

  1. harriethw


    Jun 21, 2017
    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 and use it to determine what Sprite is shown:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class SkyIoT : MonoBehaviour {
    7. //the sprites
    8.     public Sprite img1 , img2;
    11.     IEnumerator AdjustSkyToWeather() {
    12.         while (true) {
    13. //the particular URL from where I want to get this data
    14.             string weatherUrl = "";
    16.             WWW weatherWWW = new WWW (weatherUrl);
    17.             yield return weatherWWW;
    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;
    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.     }
    37.     void Start () {
    38.         StartCoroutine (AdjustSkyToWeather());
    39.     }
    40. }
    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:

    Any help would be greatly appreciated!