Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Guy_Yome · Sep 04, 2018 at 06:52 PM · arrayjsondeserialization

JSON weird deserialization

Hi. I know this subject has been asked a billion times, but I searched and tried everything I saw and can't seem to get it done. Maybe help on my specific case could enlighten me.

I have this json file (created outside c# and much longer)

 {"0":{"game":[0,0,0,0,0,0,0,0,0],"result":[1,0,0,0]},"1":{"game":[0,0,0,0,0,0,0,0,1],"result":[1,0,0,0]},"2":{"game":[0,0,0,0,0,0,0,0,2],"result":[0,0,0,1]},"3":{"game":[0,0,0,0,0,0,0,1,0],"result":[1,0,0,0]},"4":{"game":[0,0,0,0,0,0,0,1,1],"result":[0,0,0,1]},"5":{"game":[0,0,0,0,0,0,0,1,2],"result":[1,0,0,0]},"6":{"game":[0,0,0,0,0,0,0,2,0],"result":[0,0,0,1]}}

This is somewhat of an array with arrays inside (with keys of course).

I have these classes to help me deserialize it (which to me seem to be the type of data I'm trying to get).

 public class who_cares : MonoBehaviour {
     void Start () {
         // Not pertinent stuff before
         Tics results = JsonUtility.FromJson<Tics>(theJson);
     }
 }
 
 [System.Serializable]
 public class Tics {
     public Games[] values;
 }
 
 [System.Serializable]
 public class Games {
     public float[] game;
     public float[] result;
 }

Somehow, it doesn't implode nor stop execution, but just returns null. Im completely lost and have been trying and searching for hours. Thank you for anyone who dares help. :)

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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by eses · Sep 04, 2018 at 08:00 PM

Hi @Guy_Yome

You didn't mention, what JSON tool you are using. So I take a guess, that you are using built-in JsonUtility.

Your json data (if you go and test it with some online Json tool) isn't like your class structure; seems like you have an object, that contains several objects, not a list:

 {
     "0": {
         "game": [0, 0, 0, 0, 0, 0, 0, 0, 0],
         "result": [1, 0, 0, 0]
     },
     "1": {
         "game": [0, 0, 0, 0, 0, 0, 0, 0, 1],
         "result": [1, 0, 0, 0]
     },
 
 /...


So your input does not match the "pattern" of classes, arrays and fields what JsonUtility expects... and you get a empty result.

So if your classes are like this (slightly modded for my testing):

 public class JsonTest : MonoBehaviour
 {
      public TextAsset json;
     public Tics results;
 
     void Start()
     {
         results = JsonUtility.FromJson<Tics>(json.text);
     }
 }
 
 [System.Serializable]
 public class Tics
 {
     public Games[] values;
 }
 
 [System.Serializable]
 public class Games
 {
     public float[] game;
     public float[] result;
 }


And if the data was something like this (read the JsonUtility docs - it explains the limitations):

 {
     "values":
     [
         {"game":[1,0,0,0,0,0,0,0,0],"result":[1,0,0,0]},
         {"game":[2,0,0,0,0,0,0,0,0],"result":[2,0,0,0]},
         {"game":[3,0,0,0,0,0,0,0,0],"result":[3,0,0,0]}
     ]
 }


...It should read into Unity like this:

alt text



20180905-json-test.png (13.3 kB)
Comment
Add comment · Show 4 · 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 Guy_Yome · Sep 04, 2018 at 08:08 PM 0
Share

Hey! Thanks for answering! So do you think I need to use another Json tool? Im using the basic JsonUtility as you mentionned. Im gonna try to check the structure online too.

avatar image Guy_Yome · Sep 04, 2018 at 08:18 PM 0
Share

I don't get how I don't have the right structure yet. I went to check my structure online. Changed it a bit to simplify (removed the keys).

Looks like

 [{"game":[0,0,0,0,0,0,0,0,0],"result":[1,0,0,0]},{"game":[0,0,0,0,0,0,0,0,1],"result":[1,0,0,0]},{"game":[0,0,0,0,0,0,0,0,2],"result":[0,0,0,1]}]

And I got this structure for Json (which is what the tool tells me it is (array of "dictionaries" of two kind of arrays)), but still, no match.

 [System.Serializable]
 public class Tics {
     public Games[] values;
 }
 
 [System.Serializable]
 public class Games {
     public Game game;
     public Result result;
 }
 
 [System.Serializable]
 public class Game {
     public int[] game;
 }
 
 [System.Serializable]
 public class Result {
     public int[] result;
 }
avatar image eses Guy_Yome · Sep 04, 2018 at 09:04 PM 0
Share

@Guy_Yome - I'll update the answer.

avatar image Guy_Yome · Sep 05, 2018 at 03:13 PM 1
Share

Thanks man. Really appreciate the help!

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.

Follow this Question

Answers Answers and Comments

165 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

JsonUtility and Arrays - JSON must represent an object type. 0 Answers

Argument Exception: J SON must represent an object type. Serialization list of objects 0 Answers

Problem with jsonconstructor while deserializing a class 0 Answers

How to reliably send Kinect v2 data over MQTT? 1 Answer

save one big json string vs multi seperate json strings in playerprefs 0 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