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 /
avatar image
2
Question by hitwicket · Jul 29, 2016 at 12:25 PM · jsonparsing

JSONUtility creates a null object of subclass even when no data

Hi,

The JsonUtility's FromJson behaves strangely for subclass parsing..

Example:

 [Serializable]
 public class Player {

     public int id;
     public string name;
     public PlayerSkills skills;

     public static new Player createFromJSON(string jsonstring) {
         return JsonUtility.FromJson<Player>(jsonstring);
     }
  }

If the json data received from server is {"id":123, "name":"Rajat"} and the data for skills is not given, the values of the field skills is still an object of class PlayerSkills instead of being null.. and the value for fields in skills are default values, eg 0 for a int field..

How to differentiate between server sending the data of skills as value zero and server not sending the data at all..

In your documentation at https://docs.unity3d.com/Manual/JSONSerialization.html its bit ambiguous, can you explain what will be the constructed value for subclass..

If the JSON data contains values that do not map to fields in MyClass then those values will simply be ignored, and if the JSON data is missing values for fields in MyClass, then those fields will be left at their constructed values in the returned object.

We are trying to port our app form android, and the behaviour there for json parsing will the that the field will have null value if there is no data sent for it..

@superpig

Comment
Add comment · Show 1
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 Binouze · Apr 14, 2017 at 05:51 AM 0
Share

Did you find an answer ? I have the same problem. I have server side code that I cannot change and i must handle this kind of not sent values from server.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xxxmatulisxxx · May 28, 2021 at 03:37 PM

Hi, you have never constructed PlayerSkills class :)

public PlayerSkills skills; change to: public PlayerSkills skills = new PlayerSkills();

This is the default construction of class, you can make your own construction function but in C# class named functions is default construction :) I hope it will help you :)

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 Bunny83 · May 29, 2021 at 02:47 AM 0
Share

You post an answer to a question that is 5 years old? Also what you just said doesn't have anything to do with what was asked....


His problem was not that the skills field is null, but that it is not null. This however is the normal behaviour of Unity's serialization system. Sub classes are treated like structs, so they are always created by the deserializer. However since the json data does not contain any information for that class, the sub class will have its default values.


Specifically Unity's serialization system does not support "null" values except for UnityEngine.Object derived types. However those are not really supported by the JsonUtility class.


Please do not necro-post such ancient questions. The OP was last seen in 2017...

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Paring json Array object 3 Answers

Parsing Facebook graph JSON 1 Answer

Deserialize nested Json data 1 Answer

Can't read float array into C# from JSON using Simple JSON 2 Answers

Looking for Code that will Read JSON files (C# code) 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