• 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 QuinnForTheWin · Aug 23, 2019 at 08:31 PM · serializationarraysjson

Json wrapped array deserialization

I've been trying to deserialize the following Json file using the wrapper method from this answer: Serialize and Deserialize Json and Json Array in Unity. Deserializing the base objects and their normal variables into a list of objects works fine, but I can't figure out how to get the "tags" array into a normal string array within the deserialized game object, since it will have a variable number of indices.

 {
     "Items" : [
         {
             "ID": 0,
             "title": "Mage Hand",
             "description": "A simple spell used to manipulate objects through telekinesis.",
             "texture": "/Textures/Cards/Spells/ArcaneSpell01.mat",
             "type" : "SPELL",
             "tags": [ 
                 {"primary" : "arcane"} ,
                 {"primary_strength" : "1"} ,
                 {"secondary" : "force"} ,
                 {"secondary_strength" : "1"}
             ]
         },
         {
             "ID": 1,
             "title": "A Colleague",
             "description": "Another practicioner of the arcane arts. Perhaps they know something useful?",
             "texture": "/Textures/Cards/Contacts/Wizard01.mat",
             "type" : "CREATURE",
             "tags": [ 
                 {"primary" : "arcane"} ,
                 {"primary_strength" : "1"} ,
                 {"secondary" : "relationship"} ,
                 {"secondary_strength" : "1"}
             ]
         }
     ]
 }

Any help is greatly appreciated :)

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
1

Answer by Bunny83 · Aug 23, 2019 at 10:31 PM

The objects in your tags array are not the same kind of objects since the variable / field names are different. This makes it difficult / impossible to actually map the json objects to C# objects. Since json does not contain any type information for objects, deserialization can not differentiate between different class types.


It might be easier to not map them to specific C# classes but to use a generic json library like my SimpleJSON library. It can handle any kind of json structure. It provides a simple hierachical structure to access any information stored in the json text.


If you don't want to use such a library you might want to rethink about the json structure. A tag object might look like this:

 {
     "name":"primary",
     "value":"arcane"
 }

instead of

 {
     "primary" : "arcane"
 }

This way you can model any kind of "tags" and use the same C# class for all tags. If you only have those 4 tags you probably don't want to use an array at all and use a tags class. For those 4 tags I would probably do something like:

 "tags": {
     "primary": {
         "type": "arcane",
         "strength": "1"
     },
     "secondary": {
         "type": "force",
         "strength": "1"
     }
 }

Though since we don't know the exact usecase we can't decide for you.

Comment
Add comment · Show 2 · 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 QuinnForTheWin · Aug 28, 2019 at 04:00 PM 0
Share

Thank you very much for the clarification! The only thing I'm worried about with using a tag object is that I'm expecting to have a variable number of tags for each item. I can understand having a tag object that has the variables "type" and "strength", but I'm still unclear how to parse my individual items as having a variable number of tag objects within them without using an array. Is that something that is even possible without using a supplementary library such as the one you provided?

avatar image Bunny83 QuinnForTheWin · Aug 28, 2019 at 05:35 PM 0
Share

No, that's not possible without such a library since Unity's json serializer is only an object mapper. Objects in C# are statically defined and you can not add / remove variables / fields at runtime.


Apart from that Unity does not support inheritance / polymorphism for sub classes since there's no type information inside the json data so the deserializer does not know which object it should use to represent a certain object. Sub classes are always created based on the field type and therefore an array can only contain one type of objects.

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

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

117 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

Related Questions

MiniJSON Multiple Rows 1 Answer

When attempting to serialize and save a jagged array it gives me an error message 1 Answer

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

Serializating a base class array to a prefab 2 Answers

(SOLVED) Serializing different JSON objects, contained in an array, in another JSON object. (C#) 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