How exactly can I get a the latitude and longitude values from this JSON?
{"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):
Unexpected node type.
UnityEngine.JsonUtility:FromJson(String)
<OnResponse>c__Iterator0:MoveNext() (at Assets/API.cs:17)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
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!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class API : MonoBehaviour {
private const string url = "http://api.open-notify.org/iss-now.json";
// Use this for initialization
void Start () {
WWW request = new WWW (url);
StartCoroutine (OnResponse (request));
}
private IEnumerator OnResponse(WWW req) {
yield return req;
ISSData data = JsonUtility.FromJson<ISSData>(req.text);
Debug.Log (data.message);
}
}
[System.Serializable]
public class ISSData
{
public string message;
public int timestamp;
public List<Position> iss_position;
}
[System.Serializable]
public class Position
{
public string latitude;
public string longitude;
}