Search Unity

  1. We want you to join us at GDC this year! We've added new sessions to Unity Central, space is limited so sign up now!
    Dismiss Notice
  2. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  6. Be the first to take a peek at upcoming 2D Animation Preview. Drop into the forum and check it out!
    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!