• 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
0
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 this Error message in console : ArgumentException: JSON must represent an object type. Please tell me what I am doing wrong.
Comment
Add comment
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
0
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 this 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
Add comment · 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 ins$$anonymous$$d 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How to get an object inside a json JsonUtility 0 Answers

Why is JsonUtility.ToJson turning all my floats into doubles? 1 Answer

how to find if WWW Post Request is succesful? 2 Answers

Use the data received from WWW in UI 1 Answer

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges