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 jeremytm · Aug 23, 2016 at 07:39 AM · c#scripting problemjson

Can JSONUtility parse property names containing dashes/hyphens?

When using a JSON API written to the JSON API 1.0 spec, multi-word property names are dasherized, e.g. "first-name".

Using JsonUtility.FromJson – I'm having trouble accessing these properties, as C# doesn't support dasherized property names in classes or structs.

Any suggestions on how to do this within JSONUtility, or will I have to use a different JSON library?

Thanks for your time :)

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 fsalvador23 · Aug 08, 2017 at 03:14 PM 1
Share

How did you solved this? I have the same problem.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by JDelekto · Jun 08, 2019 at 01:38 AM

Another option is to use the JSON.NET library asset for Unity. You can use the JsonProperty attribute on your objects in order to specify the property name from the JSON that will be used to deserialize into your object. For example:

 [Serializable]
 public class SomeClass
 {
     [JsonProperty("some-property")]
     public string someProperty;
 }
 
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
1

Answer by fedkaball · Jun 07, 2019 at 04:33 AM

you can fix your json string request like this:

 string fix = yourJsonString.replace("hot-dog", "hotdog");

then just feed this new string to JsonUtility.FromJson

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 Bunny83 · Jun 08, 2019 at 01:04 AM 0
Share

That's a bad solution because:

  • depending on the used property names you can accidentally replace text in an actual text value rather than a property name.

  • If you generally use dashes to seperate multi word property names you would need a replace for every single one of them. That would also be quite inefficient if you have larger json text since each replace call will create a new string of your whole json text.

  • Unity's JsonUtility is just a very simple class mapper. So it's mainly meant for working with C# classes and not with arbitrary json data.


avatar image Buzzrick_Runaway · Oct 09, 2020 at 12:39 AM 0
Share

Agreed with @Bunny83 that this isn't an ideal solution, however in some cases it can be used when you know the key isn't going to turn up in other content. For instance, I use this for switching "zh-Hans" to "zhHans" (with quotes) when loading some chinese simplified localisation data.
I need the column to be a standard language code in the spreadsheet that I'm loading it from, and in the app I can modify the field titles so that the json deserialiser can understand it.

avatar image
1

Answer by Bunny83 · Jun 08, 2019 at 01:18 AM

Unity's JsonUtilty's usage is quite limited as it's just a C# class mapper and also has some restrictions specific to Unity's serialization system.


For arbitrary communication you may want to use a different library. I can recommend my SimpleJSON framework which is just a single file. Because all classes are declared partial it's easy to add additional conversion support. For example I've created SimpleJSONUnity.cs which adds direct support for some of the built in Unity types.

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 JDelekto · Jun 08, 2019 at 01:40 AM 0
Share

This single file solution looks pretty cool! Do you have an attribute to be able to remap deserialized JSON property names with characters non-confor$$anonymous$$g to the language to specific properties? That would be a really powerful addition.

avatar image Bunny83 JDelekto · Jun 08, 2019 at 02:23 AM 0
Share

No. $$anonymous$$y SimpleJSON is not an object mapper. It just parses the json data into custom dictionary / list structures and provides easy access and primitive type conversion. Object mapping doesn't also work depending on the data design. I've seen responses like this

 {
     "Bob" : {
     },
     "Alice" : {
     }
 }

Yes, I know this is a bad design. However you don't always have control over the data. If you have you can adjust it to your needs. Another example is this official API for querying trading data someone else recently wanted to read from Unity. They used full timestamps as property names which makes it impossible to map it to classes. That's why my framework just reads and parses the raw data and provides easy access to that data.

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

9 People are following this question.

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

Related Questions

How do I go about deserializing a json array? 3 Answers

Saving JSON file in Android,How to save json file in android 0 Answers

How to import a list or array of objects from a json file? 1 Answer

Spawning 3D Objects according to Data at JSON Matrix 0 Answers

How to serialize parameters from JSON? 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