Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. 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
  8. 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!