0

This is my code:

string jsonResult = System.Text.Encoding.UTF8.GetString(www.downloadHandler.data);          
Player player = JsonUtility.FromJson<Player>(jsonResult);
Debug.Log(player.testObj);

this is my 2 class objects

[Serializable]
public class Player
{
public List<PlayerInfo> testObj;

}


[Serializable]
public class PlayerInfo
{
public string username;
public string password; 
public int age;
}

jsonResult contains a json object the problem is when is use JsonUtility.FromJson it tells me :

ArgumentException: JSON must represent an object type. UnityEngine.JsonUtility.FromJson[Player] (System.String json) (at C:/buildslave/unity/build/artifacts/generated/common/modules/JSONSerialize/JsonUtilityBindings.gen.cs:25) registerScript+c__Iterator2.MoveNext () (at Assets/registerScript.cs:78) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

this is my json:

[{"id":2,"username":"hamza","password":"22","age":123456,"created_at":"2017-12-06 09:36:23","updated_at":"2017-12-06 09:36:23"},{"id":3,"username":"Hamdi","password":"1234556","age":55,"created_at":null,"updated_at":null},{"id":4,"username":"Sonia","password":"5646546556","age":25,"created_at":null,"updated_at":null}]
  • When asking questions about de-serializing jsons, always post your json. The duplicate shows why it's not working under **TROUBLESHOOTING JsonUtility:**. I suggest using [this](http://json2csharp.com/) to generate your classes from json. – Programmer Dec 06 '17 at 15:02
  • thank you i edited my question –  Dec 06 '17 at 15:04
  • The received json does not match your class. Use the link I posted above to generate the correct class. Also, your json is an array. See the duplicate for how to de-serialize json array. – Programmer Dec 06 '17 at 15:10

0 Answers0