JSON problems and IEnumerator

I’m trying to get JSON data from a server using the WWW class and IEnumerator.
I removed all mono behavior inheritance, tried to deserialize the json data to my class, but I end up with an error I’ve never seen before:
ArgumentException: JSON parse error: The document root must not follow by other values.
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at C:/buildslave/unity/build/artifacts/generated/common/modules/JSONSerialize/JsonUtilityBindings.gen.cs:25)
LoadDatabase+c__Iterator0.MoveNext () (at Assets/Scripts/LoadDatabase.cs:42)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

I’m starting to wonder if the problem is that I’m using a CoRoutine. Could that be the case?

public class VehicleIndex 
	public string ID{ get; set;}
	public string Make{ get; set;}
	public string Model{ get; set;}
	public string Year{ get; set;}
	public string Mileage{ get; set;}
Removed the constructor

public class VehicleCollection
//	public List <VehicleIndex>VehicleList{ get; set;}
	public VehicleIndex[] vehicles;

Created a new class here.

Now the sticky point

public IEnumerator GetAllVehicles()
		WWW VehicleData = new WWW ("http://localhost/CMVM/LoadVehicle.php"); 
		yield return VehicleData;

		Debug.Log (VehicleData.text);
This part shows all the JSON- all good

		string json = VehicleData.text;
Not quite sure about this..
		var cars = JsonUtility.FromJson<VehicleCollection>(json);
Can anyone give me a hand with this?


I think the problem is that your VehicleIndex class is using properties instead of fields. The documentation on JSONUtility never mentions properties. However, it does say that it only supports seralization of what Unity’s serialization system does and I’m pretty sure properties are not supported since they don’t show up on inspectors by default when you have them on MonoBehaviours.

Quoting the documentation:

Internally, this method uses the Unity serializer; therefore the type you are creating must be supported by the serializer. It must be a plain class/struct marked with the Serializable attribute. Fields of the object must have types supported by the serializer. Fields that have unsupported types, as well as private fields or fields marked with the NonSerialized attribute, will be ignored.

So I can think of 2 possible solutions to this problem.

  1. This post on the forum says that you should try putting the [SerializeField] attribute on top of each of your properties. https://forum.unity.com/threads/jsonutility-and-properties.382526/

  2. Don’t use properties, use public fields instead. All of your properties are auto-implemented anyways (default get and set) and it doesn’t look like you’re gonna be using reflection or have virtual properties that will be overridden by child classes, so there wouldn’t be any difference if you swapped them for fields. (This option is better IMO)

Hope this helps!

Edit: A third option would be to use a different JSON serialization library like Json.NET that does support property serialization by default.