• 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 cadpeople · Nov 08, 2016 at 10:41 AM · serializationjsonconvertgeneric

(SOLVED) Serializing different JSON objects, contained in an array, in another JSON object. (C#)

I receive data in my Unity application, in the form of JSON. The JSON object contains various properties; among them an array of objects. These objects contain different properties based on their type. I have simplified and generalised this example as the data is not public; but the objects in the array ("things") contain many properties, and the types of object, are very different from one another. Example:

 receivedata : {
     "type": "angry stuff",
     "things": [
         {
                     "type": "person",
             "name": "paul",
             "value": 3
         },
         {
                     "type": "person",
             "name": "richard",
             "subname": "this is text"
         },
         {
             "type": "coordinate",
             "x": 0,
             "y": 0,
         }
     ]
 }

What I'm doing now, is unpacking "receivedata" using JsonUtility.CreateFromJSON, as in various examples around the internet:

 [Serializable]
 public class JSONReceivedData
 {
     public string name;
     public OBJECT? things;
 
     public static JSONReceivedData CreateFromJSON(string jsonString)
     {
         return JsonUtility.FromJson<JSONUser>(jsonString);
     }
 }

What od I replace OBJECT? with? So far I've tried creating a json object that only has the "type" property; converting it, and then reconveritng it based on what type it is. First I tried having "things" be a string, or a string array, but both failed to convert to anything. I simple got an empty string, or string array. Then I tried having OBJECT? be a "JSONThing" object, having only a string: type property, which works, but if I then convert that JSONThing object, into fx. a JSONPerson object, I only have the type property; the other properties don't exist anymore.

So how do I go back and get the data from the original JSON string? The only way I can see is to have several overload versions of the "JSONReceivedData" class, each having the OBJECT? property, be an array of the typed objects (fx. JSONPerson), but that is not feasible, there are a lot of different types of objects, and I need to be able to do this conversion in a reasonable time, not to mention how much code this would amount to. I've also tried converting "things" to an object, an object array, a JSONObject and a JSONObject array. Each resulted in no data.

IN summary: How can I go back and ONLY get the "things" property from the original data, or convert it to a generic array, without loosing unique properties? I know this may be hard to follow, and it doesn't help I have to use a generic example, but I'm really at a loss here. Feel free to ask any questions, and I appreciate you taking the time to read all this :)

Comment

People who like this

0 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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by AbdelBous · Nov 08, 2016 at 11:20 AM

Hi,

Arrays are not supported correctly with JsonUtility. I recommend you to use newtonsoft json library, it works much better (for array and for ? type too)

Be carefull, don't use the origanal dll, but the unity-package : https://github.com/SaladLab/Json.Net.Unity3D

Comment
cadpeople
Mentoliptus
FranzVz

People who like this

3 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 cadpeople · Nov 09, 2016 at 12:35 PM 0
Share

Thanks, that's actually what someone else recomended as well :) I'm not using the Unity plugin, but I am using the 2.0 version of the newton dll. It's working very well!

avatar image djkpA cadpeople · Sep 08, 2017 at 10:47 AM 0
Share

Hello , but Json.Net.Unity3d is unable to serialize a GameObject. Is it working for you ?

avatar image

Answer by matchsun · Mar 20, 2017 at 02:20 PM

Hello, maybe you can use the following helper code to do it,

 public JsonHelper ()
         {
         }
         //Usage:
         //YouObject[] objects = JsonHelper.getJsonArray<YouObject> (jsonString);
         public static T[] getJsonArray<T>(string json)
         {
             string newJson = "{ \"array\": " + json + "}";
             Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);
             return wrapper.array;
         }
         //Usage:
         //string jsonString = JsonHelper.arrayToJson<YouObject>(objects);
         public static string arrayToJson<T>(T[] array)
         {
             Wrapper<T> wrapper = new Wrapper<T> ();
             wrapper.array = array;
             return JsonUtility.ToJson (wrapper);
         }
 
         [Serializable]
         private class Wrapper<T>
         {
             public T[] array;
         }
     }




Comment
karsnen

People who like this

1 Show 0 · 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

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

6 People are following this question.

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

Related Questions

Json deserialisation of server results? 1 Answer

Json wrapped array deserialization 1 Answer

JsonConvert.DeserializeObject Not responding when building IOS project. 0 Answers

How to protect JSON file game data? 3 Answers

Json Serialization documentation official Unity website - marking it [Serializable] caused error 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