【问题标题】:Unity3d: How to store Dictionary as object in JSON?Unity3d:如何将字典存储为 JSON 中的对象?
【发布时间】:2020-04-05 17:18:32
【问题描述】:

我是 Unity3d 和 C# 的新手。我想将一些数据存储在 JSON 文件中,以便它们在文本编辑器中易于编辑

到目前为止,我使用的是 JsonUtility,因为它是在 Unity 中构建的,而且效果很好。使用起来很简单:

string json = JsonUtility.ToJson(foo);
Foo foo = JsonUtility.FromJson<Foo>(json);

对我来说很棒。

但是现在,我添加了一些词典。它们稍后被转换为更高效的数据结构,但我想将它们序列化为 JSON 作为命名对象。

这是我的代码示例。在我的例子中,对象结构是设置和已知的。单字典保留一种类型的对象,而字典在可序列化对象的层次结构中设置得更深。

[System.Serializable]
public struct Bar{
    public int x;
    public int y;
}

[System.Serializable]
public class Foo{
    public int otherData = 43;
    public Dictionary<string,Bar> bar = new Dictionary<string,Bar>(){
        {"spaceship", new Bar{x = 7, y = 11}},
        {"pirates", new Bar{x = 5, y = -3}},
    };
}

我想要达到的目标:

{
   "otherData": 43,
   "bar": {
        "spaceship": {"x": 7, "y": 11},
        "pirates": {"x": 5, "y": -3}
   }
}

有什么办法可以做到吗?

也许是我可以为 Dictionary 编写序列化/反序列化方法的一种方式?

【问题讨论】:

    标签: c# json dictionary unity3d


    【解决方案1】:

    据此this answerJsonUtility 仅支持简单类型,并且不包括字典(答案中明确提到了这一点)。

    最好的办法是切换到Json.NET,它以NuGet package 的形式提供,它支持更复杂的用例,例如这个。

    【讨论】:

      【解决方案2】:

      使用以下代码:

       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;
          }
      

      【讨论】:

        猜你喜欢
        • 2021-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 1970-01-01
        • 2016-07-30
        • 1970-01-01
        相关资源
        最近更新 更多