Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 scarffy · Nov 05, 2020 at 02:32 AM · jsondeserialization

JSON Deserialization

Hi,

I'm still beginner in json deserialization.

I have this JSON I get.

 {
     "1": {
         "id": 1,
         "user_id": "xx",
         "booth_type": "EXPERT",
         "category": "Digital",
         "virtual_location": "G-01-201",
         "template": "B0167-3",
         "payment_status": "PAID",
         "status": null,
         "date_registered": "30/10/2020",
         "panels": {
             "1": {
                 "id": 1,
                 "type": "youtubeVideo",
                 "mime_type": "youtubeVideo",
                 "url": xx"
             },
             "2": {
                 "id": 3,
                 "type": "banner",
                 "mime_type": "image/jpeg",
                 "url": "xx"
             },
             "3": {
                 "id": 4,
                 "type": "logo",
                 "mime_type": "image/png",
                 "url": "xx"
             },
             "4": {
                 "id": 5,
                 "type": "brochure",
                 "mime_type": "application/pdf",
                 "url": "xx"
             }
         }
     },
     "2": {
         "id": 2,
         "user_id": "yy",
         "booth_type": "STANDARD",
         "category": "DIGITAL",
         "virtual_location": "G-01-202",
         "template": "B0167-2",
         "payment_status": "PENDING",
         "status": null,
         "date_registered": "30/10/2020",
         "panels": {
             "1": {
                 "id": 6,
                 "type": "poster",
                 "mime_type": "application/pdf",
                 "url": "yy"
             },
             "2": {
                 "id": 7,
                 "type": "logo",
                 "mime_type": "image/png",
                 "url": "yy"
             },
             "3": {
                 "id": 8,
                 "type": "photo",
                 "mime_type": "image/png",
                 "url": "yy"
             }
         }
     }
 }


How do I deserialize this json to object? is it possible with JSON.Utility?

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

Answer by Bunny83 · Nov 05, 2020 at 10:12 AM

No, Unity's JsonUtility can not deserialize your json object as your objects contain invalid field names in C#. While Json allows any string to be used as key in an object, when you map this object to a C# object you have to create a class with those key names as variable names. Since your key names are "0" and "1" those can not be variable names.


If you are still interested in mapping your json to actual C# objects you can use the Json.Net library and let it map those objects to a dictionary.


However if you just want to access / parse the data you could use my SimpleJSON parser. It does not map your javascript objects to C# objects but just parses them into a custom tree structure which makes it easy to work with the data they contain.

 JSONNode root = JSON.Parse(yourJsonText);
 string type = root["1"]["booth_type"]; // reads "EXPERT"
 
 JSONNode panel = root["1"]["panels"]["1"];
 string pType = panel["type"]; // reads "youtubeVideo"
 string pUrl = panel["url"]; // reads "xx"
 
 // to iterate through all panels of the second object
 foreach(var kv in root["2"]["panels"])
 {
     int id = kv.Value["id"];
     string type = kv.Value["type"];
     string url = kv.Value["url"];
     // ...
 }

Note that if you are in control of generating those json objects, you really should use arrays instead of objects. Arrays have to start with an index of 0 to be recognised as an array by most serializers (thinking about php or javascript). Arrays also must have continuous indices. Since your "keys" seem to just enumerate the objects and panels those probably should be arrays instead.


When they are actual Json arrays, Unity's JsonUtility would be back on the table. Though it has a small limitation. The first / root element has to be an object and can not be an array. So you would need to wrap your array in an object first. So your json would need to look like this:

 {
     "data" : [
         {
             "id": 1,
             "user_id": "xx",
             "booth_type": "EXPERT",
             "category": "Digital",
             "virtual_location": "G-01-201",
             "template": "B0167-3",
             "payment_status": "PAID",
             "status": null,
             "date_registered": "30/10/2020",
             "panels": [
                  {
                      "id": 1,
                      "type": "youtubeVideo",
                      "mime_type": "youtubeVideo",
                      "url": xx"
                  },
                  {
                      "id": 3,
                      // ....
                  }
             ],
         }
     ]
 }

If you can change your json to something like that you can use the JsonUtility with those classes:

 [System.Serializable]
 public class JsonResponse
 {
     public List<DataObject> data;
 }
 
 [System.Serializable]
 public class DataObject
 {
     public int id;
     public string user_id;
     public string booth_type;
     public string category;
     public string virtual_location;
     public string template;
     public string payment_status;
     public string status;
     public string date_registered;
     public List<Panel> panels;
 }
 
 [System.Serializable]
 public class Panel
 {
     public int id;
     public string type;
     public string mime_type;
     public string url;
 }

and to deserialize it you just do

 JsonResponse response = JsonUtility.FromJson<JsonResponse>(yourJsonText);

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 scarffy · Nov 06, 2020 at 03:50 AM 0
Share

Alright, the best solution is the redo the json again right? Thank you for elaborate answer

avatar image scarffy · Nov 06, 2020 at 03:31 PM 0
Share

Thank you. I'm able to deserialize them now

avatar image
0

Answer by swanne · Nov 05, 2020 at 09:19 AM

Hi,

I recommend you research and gain a good understanding of the theory behind serializing and deserializing JSON. When you understand how it works, using Unity's JSON.Utility is very simple.

There are some good videos on YouTube which explain working with JSON and the theory is the same across all platforms. When you've done that, check out the unity docs -https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html

Essentially, you need to create a class which defines the object you want to create then deserialize the JSON string to create an object and populate the attributes of the class.

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 scarffy · Nov 06, 2020 at 03:11 AM 0
Share

Thank you. I watched the tutorial but I can't get my head wrap around this json. I tried many ways but couldn't make it work.

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

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

Deserialize Json into list.,how to deserialize a list via .FromJson 1 Answer

ArgumentException: Cannot deserialize JSON to new instances of type 0 Answers

Json.net deserialization problem 1 Answer

Deserializing dictionary with 'Json.Net' for Unity returns a null object. 0 Answers

JSON deserialization 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