I'm a total noob regarding JSON, never worked with it before and I just can't figure it out. I've tried many things but I just jump around from error to error. I hope someone can help me.
so this is a sample of my JSON file, I read this file from a website.
[{"ID":60034,"Datetime":1519029071,"Module":"Krol 42","Latitude":51.8423083333333,"Longitude":4.57711,"Speed":0.59264},{"ID":58961,"Datetime":1519025476,"Module":"Krol 42","Latitude":51.8422666666667,"Longitude":4.576865,"Speed":0.59264}]
and this is the my script:
[Serializable]
public class Item
{
public int ID;
public int Datetime;
public string Module;
public float Latitude;
public float Longitude;
public float Speed;
}
public class JsonHelper
{
public static T[] getJsonArray<T>(string json)
{
string newJson = "{ \"array\": " + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);
return wrapper.array;
}
[Serializable]
private class Wrapper<T>
{
public T[] array;
}
}
IEnumerator Start()
{
string url = "http://tnt.ssedemo.eu/TrackAndTraceStrukton.aspx";
WWW www = new WWW(url);
yield return www;
if (www.error == null)
{
string jsonString = www.text;
Item[] item = JsonHelper.getJsonArray<Item> (jsonString);
Debug.Log (item[0].ID);
}
else
{
Debug.Log("ERROR: " + www.error);
}
}
and the error I'm getting now atm is:
ArgumentException: JSON parse error: Missing a comma or '}' after an object member.
I hope someone can help me, I would appreciate it a lot, thanks in advance.
[EDIT]
SOLUTION
[Serializable]
public class Item
{
public int ID;
public int Datetime;
public string Module;
public float Latitude;
public float Longitude;
public float Speed;
}
public class JsonHelper
{
public static T[] getJsonArray<T>(string json)
{
string newJson = "{ \"array\": " + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);
return wrapper.array;
}
[Serializable]
private class Wrapper<T>
{
public T[] array;
}
}
IEnumerator Start()
{
string url = "http://tnt.ssedemo.eu/TrackAndTraceStrukton.aspx";
WWW www = new WWW(url);
yield return www;
if (www.error == null){
string jsonString = www.text;
int index = jsonString.LastIndexOf("]");
if (index > 0){
jsonString = jsonString.Substring(0, index + 1);
}
Item[] item = JsonHelper.getJsonArray<Item> (jsonString);
foreach(Item krol in item){
Transform ride = Instantiate(prefab, new Vector3(krol.Longitude * 10f, 2f, krol.Latitude * 10f), Quaternion.identity) as Transform;
}
} else {
Debug.Log("ERROR: " + www.error);
}
}