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 Sun-Pengfei · Oct 24, 2016 at 03:32 AM · serializationjsondeserialization

how to deserialize json with arbitrary string keys using JsonUtility(unity c#)?

Unity, c#, use JsonUtility. Say I have a json string as follows:

 {
     "1,1":"dd",
     "2,1":"abc",
     "2,2":"123"
 }

The amount and content of keys are arbitrary. How can I deserialize this json and transfer to my own class using JsonUtility.FromJson<>()?

If the keys are fixed, then I know you can make a class with variables with the name of the keys. What to do if keys are arbitrary?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by josefnpat · Apr 27, 2020 at 12:29 AM

You cannot use JsonUtility to read arbitrary keys.

You'll have to use a library like SimpleJson.

 jsonNode = JSON.Parse(jsonString);
 Dictionary<string, string> branches = new Dictionary<string, string>();
 foreach (string branchKey in jsonNode["branches"].Keys)
 {
   branches[branchKey] = jsonNode["branches"][branchKey];
 }
 node.branches = branches;

note: there's probably a better way to convert the information via branches[key] = jsonnode[branches][key] but I just wanted to post this since there wasn't really an appropriate answer that doesn't alter the JSON source.

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

Answer by Naphier · Oct 24, 2016 at 09:42 AM

Keys can't be arbitrary. They need to be the names of the public variables in the class that you're serializing.

 public class MyClass
 {
     public string myVar1 = "yo";
 }

JSON:

 {
     "myVar": "yo"
 }

It looks like you're trying to create a 2-dimensional array of strings from the JSON data. I'm not sure if unity supports that in their json, but the class structure would be:

 public class MyClass 
 {
      string[,] myStrings;
 }

The corresponding JSON would be:

 {
   "myStrings": [
     ["a", "b", "c"],
     ["d", "e", "f"],
     ["h", "i", "j"]
   ]
 }

This might help you determine your JSON structure: http://www.jsoneditoronline.org/

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 Sun-Pengfei · Oct 24, 2016 at 12:12 PM 0
Share

Thanks for your info, I used a workaround to avoid this problem, as explained in my answer.

avatar image andybak · Feb 17, 2018 at 04:47 PM 1
Share

"$$anonymous$$eys can't be arbitrary" - well JSON keys can be arbitrary. It's JsonUtility that's the issue.

avatar image
0

Answer by Sun-Pengfei · Oct 24, 2016 at 12:10 PM

Still haven't found a way to do this. Workaround: change the JSON structure to avoid dictionary with arbitrary keys. The dictionary mentioned in the question can be changed into:

 [
     {"key":"1,1","value":"dd"},
     {"key":"2,1","value":"abc"},
     {"key":"2,2","value":"123"}
 ]
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
avatar image
0

Answer by jamesmalvi · Jun 14, 2017 at 08:52 AM

Use these tools to work with JSON Data.

  • JSON Viewer

  • JSON Formatter

  • JSON Editor

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 · Jun 14, 2017 at 11:18 AM 0
Share

That's completely irrelevant in regards to the question. This doesn't answer the question.

avatar image
0

Answer by krutikwork · Aug 11, 2020 at 10:00 AM

Hii You can also use https://onlinejsontools.org/ for json validator,beautify,minify,xml,yaml,CSV,bson,plain text,base64,tsv. Do checkout this site!

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

66 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

Related Questions

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

Json deserialisation of server results? 1 Answer

Stop specific fields from being serialized by JSON utility 3 Answers

Help with Binary serialization/de-serialization of List items and the WEBPLAYER 1 Answer

How does JsonUtility.FromJson() handle Inheritance? 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