• 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 /
avatar image
0
Question by john-essy · Apr 05, 2017 at 12:46 PM · serializationjsondeserialization

Json deserialisation of server results?

Hi there,

I have been trying for a couple hours to deserialize info from my server for our high score system. Everytime i try to i will get n error "ArgumentException: JSON must represent an object type."

This is my Json info which is generated by my server when i echo it out. What am i doing wrong?

  [
     {
         "ScoreID": "8",
         "GoogleID": "asasda",
         "Score": "258"
     },
     {
         "ScoreID": "11",
         "GoogleID": "kjjahushcjkasjkcajskndas",
         "Score": "258"
     },
     {
         "ScoreID": "10",
         "GoogleID": "aakhskjbjkabsjjkasd",
         "Score": "258"
     },
     {
         "ScoreID": "9",
         "GoogleID": "mnbabksgkajsd",
         "Score": "258"
     },
     {
         "ScoreID": "13",
         "GoogleID": "kajiuskabkjshdkhausd",
         "Score": "258"
     },
     {
         "ScoreID": "7",
         "GoogleID": "asasdas,mgkhgjhbhjaf",
         "Score": "258"
     },
     {
         "ScoreID": "6",
         "GoogleID": "ss",
         "Score": "258"
     },
     {
         "ScoreID": "5",
         "GoogleID": "kljkuasjkba",
         "Score": "258"
     },
     {
         "ScoreID": "4",
         "GoogleID": "asdadasdasdasdw",
         "Score": "258"
     },
     {
         "ScoreID": "3",
         "GoogleID": "asdadsasd",
         "Score": "258"
     },
     {
         "ScoreID": "2",
         "GoogleID": "kbaskjkjsbnkjas",
         "Score": "258"
     },
     {
         "ScoreID": "12",
         "GoogleID": "lahaushyuiahkjsjksd",
         "Score": "258"
     },
     {
         "ScoreID": "1",
         "GoogleID": "254asdasd54a5s1das2d1as54d",
         "Score": "259"
     },
     {
         "ScoreID": "14",
         "GoogleID": "kjaskjhjkahsjkdnjkasd",
         "Score": "6859"
     }
 ]

and this is the classes im trying to serialise into

 [System.Serializable]
 public class LeaderBoardEntries
 {
     public int ScoreID;
     public string GoogleID;
     public int Score;
 }
 [System.Serializable]
 public class Scores
 {
     public List<LeaderBoardEntries> scores;
 }
Comment
Add comment · Show 2
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 SohailBukhari · Apr 05, 2017 at 02:32 PM 0
Share

There is built in functionality for parsing json name "JsonUtility" which serialized/deserialized json string to object or vice versa.

 string json = JsonUtility.ToJson(myObject);

Unity Official Documentation For JSONSerialization link JSONSerialization

avatar image john-essy · Apr 05, 2017 at 04:06 PM 0
Share

Im not sure you understand what i am asking,

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by elenzil · Apr 05, 2017 at 05:08 PM

"ArgumentException: JSON must represent an object type."

an object type in json is a {}. your json is a [], which is an array.

if you're able to change the server code, modify it to emit something like

 {
   "scores": [
     {
       "ScoreID": "1",
       "GoogleID": "254asdasd54a5s1das2d1as54d",
       "Score": "259"
     },
     {
       "ScoreID": "12",
       "GoogleID": "lahaushyuiahkjsjksd",
       "Score": "258"
     }
   ]
 }

if you're not able to modify the server code, have your client modify the json string before parsing. eg

 string newJSONString = "{ \"scores\": " + oldJSONString + " }";

.. i would strongly advise trying to to it on the server, tho.

also, unrelatedly, if it were me i would rename your "LeaderBoardEntries" class to "LeaderBoardEntry".

Comment
Add comment · Show 1 · 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 elenzil · Apr 05, 2017 at 11:50 PM 0
Share

actually, the docs are pretty vague. arrays are not mentioned.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

70 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

Related Questions

Deserializing dictionary with 'Json.Net' for Unity returns a null object. 0 Answers

how to deserialize json with arbitrary string keys using JsonUtility(unity c#)? 4 Answers

EndOfStreamException : Failed to read past end of stream 1 Answer

Best Way to Store Large Number of GameObjects? 1 Answer

unity 3d loading data from json problem?! 1 Answer


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