According this this answer, JsonUtility only supports simple types, and that excludes dictionaries (this is explicitly mentioned in the answer).
Your best bet would be to switch to Json.NET, which is available as a NuGet package, which supports more complex use cases such as this one.
Answer from Vera on Stack OverflowAccording this this answer, JsonUtility only supports simple types, and that excludes dictionaries (this is explicitly mentioned in the answer).
Your best bet would be to switch to Json.NET, which is available as a NuGet package, which supports more complex use cases such as this one.
Use the following code :
static string ConvertFooClassToJson()
{
List<string> jsonStrings = new List<string>();
foreach (FieldInfo field in typeof(Foo).GetFields())
{
string fieldName = field.Name;
Foo fieldsInstant = new Foo();
var fieldValue = field.GetValue(fieldsInstant);
if (field.FieldType.IsValueType)
{
string valueType = $"{fieldName}:{fieldValue}";
jsonStrings.Add(valueType);
}
else
{
dynamic dynamicDic = fieldValue;
List<string> dicValues = new List<string>();
foreach (var dicItem in dynamicDic)
{
var itemKey = dicItem.Key;
var itemValue = dicItem.Value;
List<string> barClassLst = new List<string>();
Bar barValue = itemValue;
foreach (var barItem in barValue.GetType().GetProperties())
{
var barPropertyName = barItem.Name;
var barPropertyValue = barItem.GetValue(barValue);
barClassLst.Add($"{barPropertyName}:{barPropertyValue}");
}
var barPropertiesString = "{" + string.Join(',', barClassLst.ToArray()) + "}";
string dicString = $"{itemKey}:{barPropertiesString}";
dicValues.Add(dicString);
}
var fooString = "{" + string.Join(',', dicValues.ToArray()) + "}";
string finalString = $"{fieldName}:{fooString}";
jsonStrings.Add(finalString);
}
}
string jsonResult = "{" + string.Join(',', jsonStrings.ToArray()) + "}";
return jsonResult;
}
string Dictionary to JSON?
Need help serializing a Dictionary<string, object> using Json
c# - Unity Serializing Nested Dictionary to JSON - Stack Overflow
How can I save a Dictionary in a class to JSON? - Questions & Answers - Unity Discussions
Videos
I'm trying to figure out the easiest way to get a Dictionary<string, string> to a JSON array string format, so that for example:
Dictionary<string, string> myDict = new Dictionary<string, string>()
myDict.Add("name", "john");
myDict.Add("age", "21");is converted to
[
"name" : "john",
"age" : "21"
]and so on.
I can't seem to fine a NewtonSoft.JSON (included in Unity) call that will do that.
Is it easier to just iterate over the dictionary and build that string manually?
Trying to save one List< Dictionary< string, string [] > > and then load them when the program start.
Current kode for saving:
string json = JsonUtility.ToJson(DATA.CommandList);
File.WriteAllText(Application.persistentDataPath+FileName, json);
Will end up with a file that contains:
{ }
I was able to serialize a list of class, but when I switched it over to using dictionary of classes, then tried to serialize it didnt work. I just wanted to make sure that JsonUtility can serialize dictionary of classes. Or it could just be something wrong in my code
Is the class serializable? I would post your code for the class and save method.
No. If you want an alternative that has this feature, check out Full Serializer. It's written specifically for Unity and open source with a permissive license.
The hack is to store two separate arrays for keys and values, then load them into a Dictionary for use.