Search Unity

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={api_key}";
    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!
     
    Last edited: Mar 20, 2023
  2. mcroswell

    mcroswell

    Joined:
    Jan 6, 2010
    Posts:
    79
    I had the same errors for a while. There's a new way to do this in Unity. While my code only does the temperature, it might be useful to you if you're still interested (I know it's been over three years!)

    Let me know, and I'll paste it.
     
  3. Flapsjaak

    Flapsjaak

    Joined:
    Sep 15, 2018
    Posts:
    15
    If you could post it that would be great