Unity 3D:JSON反序列化时出错:ArgumentException:JSON必须代表一个对象类型

Unity 3D: JSON Error when deserializing : ArgumentException: JSON must represent an object type

我正在使用以下内容:

[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;
}

反序列化以下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 } ] } ]

而且我在控制台中收到此错误消息:ArgumentException:JSON 必须代表一个对象类型。请告诉我我做错了什么。

您的 json 以 [ 开头并以 ] 结尾,这表明它是一个数组,而不是一个对象。

当您尝试将其反序列化为 对象 根对象时,它失败了,因为它期望使用 {} 的对象注释。

您可以通过将数组更改为对象来解决此问题。 或者将其反序列化为数组。

您可以在这个答案中找到一些样本和更多解释: