Search Unity

Deserializing JSON with JSONUltility: Lists?

Discussion in 'Scripting' started by PolyMars, Oct 20, 2019.

  1. PolyMars

    PolyMars

    Joined:
    Jun 25, 2017
    Posts:
    2
    How exactly can I get a the latitude and longitude values from this JSON?
    Code (CSharp):
    1. {"iss_position": {"longitude": "99.3445", "latitude": "-50.7022"}, "message": "success", "timestamp": 1571583963}
    I've attached the code I am using to fetch and parse the JSON. It can display the message and timestamp values from the JSON just fine, but when doing that I do get this error (despite it working):

    Code (CSharp):
    1.  
    2. Unexpected node type.
    3. UnityEngine.JsonUtility:FromJson(String)
    4. <OnResponse>c__Iterator0:MoveNext() (at Assets/API.cs:17)
    5. UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
    6.  
    Any method I have tried of displaying a string in the iss_position list has failed. In addition, when I use Count to display the length of the list, it returns zero, so I assume I'm doing something very wrong. If anyone could point me in the right direction, I'd really appreciate it. Thanks!

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class API : MonoBehaviour {
    7.     private const string url = "http://api.open-notify.org/iss-now.json";
    8.     // Use this for initialization
    9.     void Start () {
    10.         WWW request = new WWW (url);
    11.         StartCoroutine (OnResponse (request));
    12.     }
    13.  
    14.     private IEnumerator OnResponse(WWW req) {
    15.         yield return req;
    16.         ISSData data = JsonUtility.FromJson<ISSData>(req.text);
    17.         Debug.Log (data.message);
    18.     }
    19. }
    20.  
    21. [System.Serializable]
    22. public class ISSData
    23. {
    24.     public string message;
    25.     public int timestamp;
    26.     public List<Position> iss_position;
    27. }
    28.  
    29. [System.Serializable]
    30. public class Position
    31. {
    32.     public string latitude;
    33.     public string longitude;
    34. }
    35.  
    36.  
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    The long and lat aren't a list, they are two floats which you would place in a class. This should do it for you;
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class IssData : MonoBehaviour
    7. {
    8.     public IssInfo Info;
    9.  
    10.     private IEnumerator Start()
    11.     {
    12.         var www = new UnityWebRequest("http://api.open-notify.org/iss-now.json")
    13.         {
    14.             downloadHandler = new DownloadHandlerBuffer()
    15.         };
    16.  
    17.         yield return www.SendWebRequest();
    18.  
    19.         if (www.isNetworkError || www.isHttpError)
    20.         {
    21.             Debug.Log("Error Loading ISS Data");
    22.             yield break;
    23.         }
    24.  
    25.        Info = JsonUtility.FromJson<IssInfo>(www.downloadHandler.text);
    26.     }
    27. }
    28.  
    29. [Serializable]
    30. public class IssInfo
    31. {
    32.     public IssPosition iss_position;
    33.     public string message;
    34.     public int timestamp;
    35. }
    36.  
    37. [Serializable]
    38. public class IssPosition
    39. {
    40.     public float longitude;
    41.     public float latitude;
    42. }
     
  3. PolyMars

    PolyMars

    Joined:
    Jun 25, 2017
    Posts:
    2
    Thanks so much! Everything works as intended now and I understand what I was doing wrong. I really appreciate your help!
     
  4. zalogic

    zalogic

    Joined:
    Oct 6, 2010
    Posts:
    273
    Still that error shouldn't happen. The field should just be ignored if the field type doesn't match like "FromJson" does for other situations.
    Please vote this issue here:
    https://issuetracker.unity3d.com/is...ng-jsonutility-dot-fromjsonoverwrite-function

    This error can happen if for example you get a malformed json and you want to handle the exception gracefully in your code by catching it. But this error is not catch-able.