- Home /
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 :)
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;
}
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
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.
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.
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.
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.
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
