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
0
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
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
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
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 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
1

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
Add comment · 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.

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

Best Way to Store Large Number of GameObjects? 1 Answer

JsonData to Enum 2 Answers

unity 3d loading data from json problem?! 1 Answer

MiniJSON Multiple Rows 1 Answer

JSON - Load Subset of Files Based on Key in File 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