I'm trying to parse this string:
[{"busses":[{"bus":1,"time":"2016-10-11 04:56:01","lat":"30.5198498","lon":"-90.47052981","acc":"6.0\n"},{"bus":2,"time":"2016-10-11 11:37:55","lat":"30.51764384","lon":"-90.47189947","acc":"3.0\n"},{"bus":3,"time":"2016-10-05 03:52:11","lat":"30.51982784","lon":"-90.47063428","acc":"8.0\n"},{"bus":4,"time":"2016-10-11 11:37:56","lat":"30.51998849","lon":"-90.47060976","acc":"3.0\n"}]}]
Here is my old code...:
public class Bus
{
public string busNum { get; set; }
public string busTime { get; set; }
public string busLat { get; set; }
public string busLon { get; set; }
public string busAcc { get; set; }
}
public class Busses
{
public List<Bus> busses { get; set; }
}
public class TestCoordinate : MonoBehaviour
{
private float nextActionTime = 0.0f;
private float period = 5.0f;
void Start()
{
}
void Update()
{
if (Time.time > nextActionTime)
{
nextActionTime = Time.time + period;
StartCoroutine("GetCoordinatesUpdate");
}
}
IEnumerator GetCoordinatesUpdate()
{
WWW www = new WWW("Myurl.com");
yield return www;
string cleanurl = www.text.Replace("(", "").Replace(")", "");
string finalurl = "[{\"busses\":" + cleanurl + "}]";
Debug.Log(finalurl);
Busses busses = JsonMapper.ToObject<Busses>(finalurl);
}
}
EDIT: After referencing the link: Serialize and Deserialize Json and Json Array in Unity, I've changed my code to this, please see my error below:
public class Bus
{
public string bus;
public string Time;
public string Lat;
public string Lon;
public string Acc;
}
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = UnityEngine.JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
public static string ToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return UnityEngine.JsonUtility.ToJson(wrapper);
}
[Serializable]
private class Wrapper<T>
{
public T[] Items;
}
}
public class TestCoordinate : MonoBehaviour
{
private float nextActionTime = 0.0f;
private float period = 5.0f;
public Bus[] busInstance;
public string lat;
void Start()
{
}
void Update()
{
if (Time.time > nextActionTime)
{
nextActionTime = Time.time + period;
StartCoroutine("GetCoordinatesUpdate");
}
}
IEnumerator GetCoordinatesUpdate()
{
//string url = "http://gdata.selu.edu/traxx/allpositions2.php";
WWW www = new WWW("http://gdata.selu.edu/traxx/allpositions2.php");
yield return www;
string cleanurl = www.text.Replace("(", "").Replace(")", "").Replace("\n","");
string finalurl = "{\"busses\":" + cleanurl + "}";
Debug.Log(finalurl);
busInstance = JsonHelper.FromJson<Bus>(finalurl);
lat = busInstance[0].Lat;
Debug.Log(lat);
}
}
Note:However now I'm getting an error saying "NullReferenceException: Object reference not set to an instance of an object" when trying to display the lat. I also tried busInstance.Length, and I receive the same error. What's wrong, am I missing something simple that I'm not seeing?
I'm trying to get the 4 busses into a list using the JsonMapper.ToObject from LitJson.
I've referenced this post: how to parse json array using Litjson? ...And that post is what I've built this on. However, I'm getting an error saying "Type Busses can't act as an array."
You will see that I've concatenated "[{busses:" to the string in order to have it resemble the post I referenced.
Note: I'm using LitJson and Unity.