Unity:我无法从 unity 项目的 json 文件中获取数据,特别是对象嵌套在对象中

问题描述 投票:0回答:2

所以我需要将一个 json 文件放到字典中,这样我就可以访问里面的位置值,但是我似乎无法全神贯注地围绕格式化类来获取数据。这是json

{
  "A-1_Dynamic": {
    "models": {
      "Obj_TreeConiferous_A_Snow_01": {
        "positions": [
          {
            "location": [ -4046.613525390625, 300.58489990234375, -3327.34228515625 ],
            "rotate": [ 0.0, 3.006002426147461, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4069.423583984375, 304.7637634277344, -3353.51416015625 ],
            "rotate": [ 0.0, 1.8161119222640991, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4074.988037109375, 190.91162109375, -3061.863037109375 ],
            "rotate": [ 0.0, 0.679896891117096, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4226.9140625, 241.1768798828125, -3059.42578125 ],
            "rotate": [ 0.0, 0.0, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4208.6279296875, 233.5629425048828, -3126.795654296875 ],
            "rotate": [ 0.0, 0.0, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4131.59814453125, 158.98818969726562, -2963.489990234375 ],
            "rotate": [ 0.0, -1.3199999332427979, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4032.2548828125, 296.8008117675781, -3253.56396484375 ],
            "rotate": [ 0.0, 0.5199999809265137, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4096.81982421875, 166.0074462890625, -2994.40380859375 ],
            "rotate": [ 0.0, -1.2400000095367432, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -3995.435302734375, 319.0335998535156, -3290.858154296875 ],
            "rotate": [ 0.0, 2.2090237140655518, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4063.25341796875, 183.0584716796875, -3037.53955078125 ],
            "rotate": [ 0.0, -1.3199999332427979, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4242.64453125, 282.1507873535156, -3130.758056640625 ],
            "rotate": [ 0.0, 0.645487904548645, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4249.56884765625, 265.62750244140625, -3092.826904296875 ],
            "rotate": [ 0.0, 0.0, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4134.4814453125, 199.40121459960938, -3097.57861328125 ],
            "rotate": [ 0.0, 5.253152370452881, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4211.60888671875, 237.02320861816406, -3134.893310546875 ],
            "rotate": [ 0.0, 0.0, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4245.36767578125, 278.0252685546875, -3121.50634765625 ],
            "rotate": [ 0.0, 0.820020854473114, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -3982.287841796875, 334.6233825683594, -3362.15966796875 ],
            "rotate": [ 0.0, 5.689845561981201, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4270.5146484375, 234.09169006347656, -3019.659423828125 ],
            "rotate": [ 0.0, 0.0, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4195.93701171875, 246.44842529296875, -3165.1669921875 ],
            "rotate": [ 0.0, 0.0, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4171.91650390625, 242.51864624023438, -3224.1806640625 ],
            "rotate": [ 0.0, -1.2400000095367432, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4057.538330078125, 301.52459716796875, -3336.35546875 ],
            "rotate": [ 0.0, 0.12114069610834122, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4141.931640625, 165.43731689453125, -2977.140380859375 ],
            "rotate": [ 0.0, -1.2400000095367432, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4203.0791015625, 236.73880004882812, -3142.16357421875 ],
            "rotate": [ 0.0, 0.0, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4073.278564453125, 155.88107299804688, -2943.0166015625 ],
            "rotate": [ 0.0, -1.2400000095367432, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4059.146484375, 307.46551513671875, -3350.83154296875 ],
            "rotate": [ 0.0, 3.614266872406006, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4141.1943359375, 159.79676818847656, -2953.390625 ],
            "rotate": [ 0.0, 0.6000000238418579, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4140.79541015625, 204.42941284179688, -3118.21630859375 ],
            "rotate": [ 0.0, 3.053586006164551, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4163.22998046875, 192.46229553222656, -3035.843017578125 ],
            "rotate": [ 0.0, 0.0, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4004.206787109375, 331.18939208984375, -3360.05419921875 ],
            "rotate": [ 0.0, 5.680851936340332, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4081.77880859375, 285.52581787109375, -3301.23388671875 ],
            "rotate": [ 0.0, 0.0, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4036.37890625, 296.74365234375, -3245.054443359375 ],
            "rotate": [ 0.0, -0.6800000071525574, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4135.1318359375, 168.22479248046875, -2990.285888671875 ],
            "rotate": [ 0.0, 0.0, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4077.190185546875, 184.27197265625, -3047.604736328125 ],
            "rotate": [ 0.0, 1.442330241203308, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4033.4873046875, 315.0357360839844, -3347.161865234375 ],
            "rotate": [ 0.0, -1.3199999332427979, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4161.57958984375, 247.22796630859375, -3236.60986328125 ],
            "rotate": [ 0.0, 5.881755828857422, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4020.97119140625, 300.33905029296875, -3206.254638671875 ],
            "rotate": [ 0.0, 0.0, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4075.729736328125, 294.3045654296875, -3332.35546875 ],
            "rotate": [ 0.0, 3.731034278869629, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4003.70068359375, 314.2876892089844, -3301.96533203125 ],
            "rotate": [ 0.0, 1.0399999618530273, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -3987.53466796875, 324.06951904296875, -3302.6025390625 ],
            "rotate": [ 0.0, 2.8772897720336914, 0.0 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          }
        ]
      },
      "Obj_FlowerVioletCliff_A_01": {
        "positions": [
          {
            "location": [ -4080.424560546875, 483.92034912109375, -3733.233154296875 ],
            "rotate": [ -1.8166382312774658, -0.30297359824180603, 3.120814561843872 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4243.26171875, 303.47265625, -3268.234375 ],
            "rotate": [ 1.3795164823532104, 1.4161375761032104, 0.3972167670726776 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          },
          {
            "location": [ -4255.08984375, 315.87109375, -3244.0 ],
            "rotate": [ -0.8560790419578552, 0.60546875, -2.792602300643921 ],
            "scale": [ 1.0, 1.0, 1.0 ]
          }
        ]
      }
    }
  }
}

这就是我所拥有的

public class sceneCreator : MonoBehaviour{
    public TextAsset textJson;
    public RootData A1Data = new RootData();

    void Start(){
        A1Data = JsonUtility.FromJson<RootData>(textJson.);
    }
}

[System.Serializable]
public class PositionData
{
    public float[] location;
    public float[] rotate;
    public float[] scale;
}

[System.Serializable]
public class ModelData
{
    public PositionData[] positions;
}

[System.Serializable]
public class A1DynamicData
{
    public Dictionary<string, ModelData> models;
}

[System.Serializable]
public class RootData
{
    public A1DynamicData A_1_Dynamic;
}

我试图改变根。因为我添加了一个额外的、不需要的部分。但是没有数据被读取。

c# json unity3d
2个回答
1
投票

对于初学者

A-1_Dynamic
!=
A_1_Dynamic
。 JSON 名称必须完全匹配 => 您不能为此使用
JsonUtility
,因为您不能在
-
 的字段名称中使用 
c#

您需要更改 JSON 中的命名或使用外部工具,如

Newtonsoft Json.NET
,它允许使用
[JsonProperty]
属性覆盖 JSON 名称。

此外,您不能使用

JsonUtility
,因为它使用不支持的Unity Serializer
Dictionary

=> 再次使用

Newtonsoft Json.NET
,它通过包管理器作为 包出现

A1Data = JsonConvert.DeserializeObject<RootData>(textJson.text);

...


public class RootData
{
    [JsonProperty ("A-1_Dynamic")]
    public A1DynamicData A_1_Dynamic;
}

0
投票

我使用 https://json2csharp.com/ 获取类和 Newtonsoft Json 包以将所有内容放入我可以访问的类中。

© www.soinside.com 2019 - 2024. All rights reserved.