• 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
Question by r4t5 · Mar 06, 2021 at 07:30 AM · serializationjsonparse

Parsing "complex" json

Hi all, I have a json file which have several entries types, how should I parse it?

 [
   {
     "Name":"John",
     "Health":3,
     "Type":"Warrior",
     "ExtendedData":{
       "Shield":1,
       "Damage":4
     }
   },
   {
     "Name":"Klara",
     "Health":4,
     "Type":"Witch",
     "ExtendedData":{
       "Magic":5,
       "Mana":4
     }
   }
 ]

basically the Extended Data may be different for each entry

thanks

Comment
Bunny83

People who like this

1 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

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Mar 06, 2021 at 09:39 AM

Well, one part is the parsing which is kinda trivial with a generic parser. However if you want to represent this data with C# classes it would be tricky. My SimpleJSON parser can parse any valid json and provides easy access to the data. It does not map the data to your own custom classes but just provides the data in the same structure only using the internal node classes. This allows direct access of any data that may be stored in the JSON.


Of course how you treat the different "extended data" sub classes is up to you. If they are specific for each "Type", you can map the data manually to your own classes. In this case you need to manually create the actual classes depending on the "Type" field. Once that's done, you could define an interface / abstract class that handles the deserialization (and serialization) for each individual class. I've posted several examples over here.

Comment
r4t5
andrew-lukasik

People who like this

2 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 Bunny83 · Mar 06, 2021 at 09:45 AM 0
Share

Note that you only need the SimpleJSON.cs file for the general parsing. However there are extension files which just need to be copied into your project as well which extend the support for other types. There's one for common Unity types like Vectors, but also one for common .NET types. As I explained in the answer I've linked above, you can create your own extention for types in your project which simplifies the usage. So you could implement a conversion operator that can convert a JSONNode into your base class type / interface and have it create the proper class depending on the "Type" field. Then just call your Deserialize method and pass the JSONNode along so each class can initialize itself from the json data.


It would be pointless to go more into detail here as it highly depends on your class structure on the C# side.

avatar image r4t5 Bunny83 · Mar 06, 2021 at 03:02 PM 0
Share

thanks man!

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

120 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

Related Questions

Stop specific fields from being serialized by JSON utility 3 Answers

How to protect JSON file game data? 3 Answers

Json Serialization documentation official Unity website - marking it [Serializable] caused error 1 Answer

How does JsonUtility.FromJson() handle Inheritance? 2 Answers

Is it possible to executing object serialization into JSON asynchronously? 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