JsonReaderException: Additional text encountered after finished reading JSON content: <. Path '', line 3, position 0

时间:2018-02-15 12:06:45

标签: c# json unity3d

I am totally new to json and I have this issue and I just can't figure it out, I do get the text from the website no problem but once I try to put it in a list I get the message: JsonReaderException: Additional text encountered after finished reading JSON content: <. Path '', line 3, position 0. this is a json sample as to how it looks:

[{"ID":50769,"Datetime":1518682266,"Module":"Krol 42","Latitude":51.8921433333333,"Longitude":4.52179666666667,"Speed":11.26016},{"ID":50767,"Datetime":1518682255,"Module":"Krol 42","Latitude":51.8916866666667,"Longitude":4.52214833333333,"Speed":22.52032}]

and here is my code

 [System.Serializable]
 public class Item
 {
     public int ID { get; set;}
     public int Datetime { get; set;}
     public string Module { get; set;}
     public float Latitude { get; set;}
     public float Longitude { get; set;}
     public float Speed { get; set;}
 }

 IEnumerator Start()
 {
     string url = "blabla.com";
     WWW www = new WWW(url);

     yield return www;

     if (www.error == null)
     {

         string jsonString = www.text;
         List<Item> list = JsonConvert.DeserializeObject<List<Item>>(jsonString);

         Debug.Log (list.Count);
     }
     else
     {
         Debug.Log("ERROR: " + www.error);
     }        
 }

I hope someone could help me, thanks in advance

[EDIT]

so I went to the duplicate and this is what I have now, its just still not doing what I want :(

[Serializable]
public class Item
{
    public int ID;
    public int Datetime;
    public string Module;
    public float Latitude;
    public float Longitude;
    public float Speed;
}

public static class JsonHelper
{
    public static T[] FromJson<T>(string json)
    {
        Wrapper<T> wrapper = 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 JsonUtility.ToJson(wrapper);
    }

    public static string ToJson<T>(T[] array, bool prettyPrint)
    {
        Wrapper<T> wrapper = new Wrapper<T>();
        wrapper.Items = array;
        return JsonUtility.ToJson(wrapper, prettyPrint);
    }

    [Serializable]
    private class Wrapper<T>
    {
        public T[] Items;
    }
}

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.FromJson<Item>(jsonString);
        Debug.Log (item[0].ID);
    }
    else
    {
        Debug.Log("ERROR: " + www.error);
    }        
}

now I get this error:

ArgumentException: JSON parse error: The document root must not follow by other values.

0 个答案:

没有答案