- Home /
Question by
Wisearn · Nov 15, 2016 at 03:05 PM ·
json5.3deserializeserializer
JsonUtility issue? Trying to deserialize to common field class.
Following: "Using FromJson() when the type is not known ahead of time
Deserialize the JSON into a class or struct that contains ‘common’ fields, and then use the values of those fields to work out what actual type you want. Then deserialize a second time into that type."
I have this:
public enum PackageTypes {
NONE,
set_endpoints,
ready_check,
ready_check_response
}
[Serializable]
public class PackageType {
public PackageTypes type;
public PackageType(PackageTypes _type) {
this.type = _type;
}
public PackageType() { }
}
{"type":"set_endpoints","data":{"services":{"matching":{"ip":"127.0.0.1","port":8080},"gamestream":{"ip":"127.0.0.1","port":3000},"events":{"ip":"127.0.0.1","port":5000}}}}
PackageType _pT = JsonUtility.FromJson<PackageType>(_jsonString);
Debug.Log(_pT.type);
outputs: NONE
should be: set_endpoints
Comment
I believe it's because the structure is a dictionary, and JsonUtility doesn't support program$$anonymous$$g.
Your answer
Follow this Question
Related Questions
Json Deserialize to dictionary[Solved] 1 Answer
[JsonUtility] cannot deserialize json to new instances of type X 2 Answers
Problem deserialize Json api rest 1 Answer
JSON problems and IEnumerator 1 Answer
JSON invalid value 1 Answer