• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 03, 2019 at 09:15 AM by Kaustav_Brainium for the following reason:

The question is answered, right answer was accepted

avatar image
Question by Kaustav_Brainium · Apr 03, 2019 at 07:00 AM · objectwwwserializationjson

JSON Error : ArgumentException: JSON must represent an object type.

I am using the following script:

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

For the following JSON data:

[

{ "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 t$$anonymous$$s Error message in console : ArgumentException: JSON must represent an object type. Please tell me what I am doing wrong.
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by JDelekto · Apr 03, 2019 at 07:59 AM

At first glance, it looks like your JSON defines an array of RootObject objects, but you are trying to deserialize a single RootObject. If you are always getting an object with t$$anonymous$$s JSON and only expect one object, you have two choices: a) remove the '[' and ']' array designator from the JSON string and deserialize as a RootObject or deserialize the JSON as a RootObject array (i.e. RootObject[]) and always take the first element of the array (if its length is not zero).

Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kaustav_Brainium · Apr 03, 2019 at 09:14 AM 0
Share

Thank You!

avatar image Bunny83 · Apr 03, 2019 at 10:09 AM 0
Share

Note that (up to this point) the Unity's JsonUtility does not support an array as root object. It always expects an object as root element. If you want / need an array and want to use Unity's JsonUtility, you have to wrap your array in an object.


For more questions about Unity's JsonUtility, have a look at this SO question. If you're just looking for a way to easily read arbitrary json, have a look at my SimplyJSON framework

avatar image JDelekto Bunny83 · Apr 03, 2019 at 10:43 AM 0
Share

Interesting, I did not know Unity originally lacked the support for the collections. Normally I would not do what I suggested first, which was removing the square brackets but would instead deserialize to a collection if the server was handing it off that way.

The SO link you provided was great in the context of Unity; however, it is good to know that they now support arrays in Unity's basic class.

I typically dislike wrapping the array as a property of some other object unless it was done with a generic so as to limit the number of classes in a project if I have several different serialized types.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is JsonUtility.ToJson turning all my floats into doubles? 2 Answers

Adding data to a set of transforms 1 Answer

(SOLVED) Serializing different JSON objects, contained in an array, in another JSON object. (C#) 2 Answers

Serialize Fields converted to object 2 Answers

JsonUtility and Arrays [Error - "JSON must represent an object type"] 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges