-1

I am using the following:

[System.Serializable]
public class Choice
{
public string choice;
public int votes;
}

[System.Serializable]
public class RootObject
{
public string question;
public string published_at;
public List<Choice> choices;
}

To deserialize the following JSON:

[ { "question": "Favourite programming language?", "published_at": "2015-08-05T08:40:51.620Z", "choices": [ { "choice": "Swift", "votes": 2048 }, { "choice": "Python", "votes": 1024 }, { "choice": "Objective-C", "votes": 512 }, { "choice": "Ruby", "votes": 256 } ] } ]

And I am getting this Error message in console : ArgumentException: JSON must represent an object type. Please tell me what I am doing wrong.

  • JsonUtility supports objects as top level nodes, so if you have an array of objects iterate thru each of them, you can't parse an array as it is. @Kaustav Dasgupta – MS90 Apr 03 '19 at 07:40

1 Answers1

0

Your json begins with an [ and ends with a ] this indicates that it is an array, and not an object.

As you are trying to deserialize it to the object rootobject, it fails, as it would expect an object annotation using { and }.

You can fix this by either changing the array to an object. Or by deserializing it to an array instead.

You can find some samples, and more explanation over at this answer: Serialize and Deserialize Json and Json Array in Unity

MX D
  • 2,453
  • 4
  • 35
  • 47