在Unity中可以反序列化字典吗? [英] Is it possible to deserialize a dictionary in Unity?
问题描述
使用如下所示的JSON文件,
With a JSON file that looks like this,
{
"dict": {"key": "value"}
}
如何在Unity中制作一个像这样的{{key:" value}的名为"dict"的字典?
how can I make a dictionary named "dict" that looks like this one {"key": "value"} in Unity?
这是我的课程.
[Serializable]
public class MyJson
{
public Dictionary<string,string> dict;
}
我不知道为什么,但是当我尝试使用此类和JsonUnility.FromJson()方法时,它似乎并没有将字典更改为我期望的字典.相反,它只是null.
I don't know why but when I try to use this class and the JsonUnility.FromJson() method it doesn't seem to change dict to that dictionary that I would expect. Instead it is just null.
我已经看到在Unity文档中它说不支持字典",所以有一些便捷的方法可以轻松地创建字典,或者我必须自己将其拼凑在一起数据?
I've seen that in the Unity docs it says "dictionaries are not supported" so is there some convenient method to create the dictionary easily or will I have to piece it together myself with the data?
这是潜在的JSON.我遇到的另一个问题是,我用python创建了JSON,这意味着我在字典中可以具有不同的值类型,但是这不适用于C#,因此现在它仅是字符串,我将后来想出一些办法.(可能将所有列表都列出来,那么只有字符串的列表将只有1个元素)
Here is a potential JSON. This kind of gets into another problem I was having though which is that I created the JSON with python meaning I could have different value types in my dictionary, but that won't work for C# so for now it's all just strings and I'll figure something out for that later. (Probably by making it all lists then the ones that are only strings will just have 1 element)
{
"pages": [
{
"files": "[null, null, null, null]",
"imagepath": "C:/Users/Jeff/Pictures/location_sharing.png",
"pageNum": "0",
"polygons": "[[[122, 184], [178, 180], [174, 159], [126, 158]], [[191, 157], [194, 185], [241, 183], [246, 154]], [[334, 219], [323, 236], [323, 247], [338, 259], [358, 259], [364, 236], [356, 221], [344, 215]], [[512, 221], [503, 235], [508, 256], [518, 267], [538, 260], [548, 250], [541, 228], [529, 218]]]"
},
{
"files": "[null, null, null]",
"imagepath": "C:/Users/Jeff/Pictures/node.png",
"pageNum": "1",
"polygons": "[[[189, 149], [181, 152], [188, 170], [197, 182], [196, 173], [192, 153]], [[233, 176], [258, 171], [244, 144]], [[116, 260], [143, 227], [181, 210], [225, 202], [276, 199], [305, 210], [353, 222], [360, 252], [361, 266]]]"
}]}
推荐答案
您更新后的示例架构看起来根本不是字典.因为 pages
中的每个元素都不是实际的嵌套json,而是一个看起来像它的字符串可以作为嵌套json插入,所以您需要做的就是
Your updated example schema does not look like it is a dictionary at all. Because each element in pages
is not actual nested json but a string that looks like it could be interpeted as nested json all you need to do is have
[Serializable]
public class PageContainer
{
Page[] pages;
}
[Serializable]
public class Page
{
public string files;
public string imagepath;
public string pagenum;
public string polygons;
}
然后做
PageContainer pageContainer = JsonUtility.FromJson<PageContainer>(yourString);
然后,您可能需要做进一步的操作
You would then potentially need to do further
FileContainer fileContainer = JsonUtility.FromJson<FileContainer>(pageContainer.files);
PolygonContainer polygonContainer = JsonUtility.FromJson<PolygonContainer>(pageContainer.polygons);
并根据需要制作必要的类.
and make the necessary classes as needed.
这篇关于在Unity中可以反序列化字典吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!