2

我正在与这个例外作斗争。我正在尝试从 json 文件中获取一些信息并从中获取一些 C# 对象。但由于某些原因,Unity 不允许我反序列化它。

这是我的 C# 代码:

IEnumerator SendRequest(string urlParam, Action<UnityWebRequest> callback)
    {
        using (webRequest = UnityWebRequest.Get(urlParam))
        {
            yield return webRequest.SendWebRequest();
            callback(webRequest);

            if (webRequest.isNetworkError || webRequest.isHttpError)
                Debug.LogError(webRequest.error);
            //else Debug.Log("Command has been performed successfully");
        }
    }

public void StartTestSuite2()
    {
        StartCoroutine(SendRequest("http://localhost:8086/down|PopOut1", (UnityWebRequest req) =>
        {
            if (req.isNetworkError || req.isHttpError)
            {
                Debug.Log($"{req.error}: {req.downloadHandler.text}");
            }
            else
            {
                Value[] values = JsonConvert.DeserializeObject<Value[]>(req.downloadHandler.text);

                foreach (Value value in values)
                {
                    Debug.Log(value.x);
                }
            }
        }));
    }

我已经用谷歌搜索了它,发现有些人有同样的问题,但我不知道如何在这里解决我的问题。我试过反序列Value化而不是Value[],它确实有效。

但是我不能用foreach(我需要)通过我的桌子

谁能帮帮我?谢谢,

编辑:这是我的“价值”类型:

public class Value
{
    public string name;
    public string type;
    public float x;
    public float y;
    public int instanceID;
}

这是我的 json 文件:

{
    "scene": "UI",
    "gameObjectDefinitions": [{
        "name": "Image",
        "type": "UnityEngine.RectTransform",
        "x": 314.79595947265627,
        "y": 327.6845397949219,
        "instanceID": 28548,
        "buttons": [],
        "children": []
    }, {
        "name": "TopFade",
        "type": "UnityEngine.RectTransform",
        "x": 26.790924072265626,
        "y": 326.97796630859377,
        "instanceID": 28614,
        "buttons": ["TopFade (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 26.790924072265626,
            "y": 326.97796630859377,
            "instanceID": 28342,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "Button (1)",
        "type": "UnityEngine.RectTransform",
        "x": 81.19093322753906,
        "y": 327.207763671875,
        "instanceID": 28600,
        "buttons": ["Button (1) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 81.19093322753906,
            "y": 327.207763671875,
            "instanceID": 28792,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "Button (2)",
        "type": "UnityEngine.RectTransform",
        "x": 135.5909423828125,
        "y": 327.207763671875,
        "instanceID": 28506,
        "buttons": ["Button (2) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 135.5909423828125,
            "y": 327.207763671875,
            "instanceID": 28576,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "Button (3)",
        "type": "UnityEngine.RectTransform",
        "x": 602.7999877929688,
        "y": 327.207763671875,
        "instanceID": 28628,
        "buttons": ["Button (3) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 602.7999877929688,
            "y": 327.207763671875,
            "instanceID": 28450,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "Button (4)",
        "type": "UnityEngine.RectTransform",
        "x": 548.4000244140625,
        "y": 327.207763671875,
        "instanceID": 28520,
        "buttons": ["Button (4) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 548.4000244140625,
            "y": 327.207763671875,
            "instanceID": 28540,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "Button1Panel",
        "type": "UnityEngine.RectTransform",
        "x": 26.808990478515626,
        "y": 287.9584045410156,
        "instanceID": 28724,
        "buttons": [],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 26.808990478515626,
            "y": 287.9584045410156,
            "instanceID": 28662,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "Button2Panel",
        "type": "UnityEngine.RectTransform",
        "x": 81.17160034179688,
        "y": 287.9584045410156,
        "instanceID": 28742,
        "buttons": [],
        "children": [{
            "name": "Text (1)",
            "type": "UnityEngine.RectTransform",
            "x": 81.17160034179688,
            "y": 287.9584045410156,
            "instanceID": 28372,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "Button3Panel",
        "type": "UnityEngine.RectTransform",
        "x": 135.55799865722657,
        "y": 287.9584045410156,
        "instanceID": 28332,
        "buttons": [],
        "children": [{
            "name": "Text (2)",
            "type": "UnityEngine.RectTransform",
            "x": 135.55799865722657,
            "y": 287.9584045410156,
            "instanceID": 28476,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "PopOutMaster",
        "type": "UnityEngine.RectTransform",
        "x": 835.2000122070313,
        "y": 198.75999450683595,
        "instanceID": 28776,
        "buttons": [],
        "children": [{
            "name": "PopOut1",
            "type": "UnityEngine.RectTransform",
            "x": 684.739990234375,
            "y": 198.75999450683595,
            "instanceID": 28310,
            "buttons": ["PopOut1 (UnityEngine.UI.Button)"],
            "children": [{
                "name": "Text",
                "type": "UnityEngine.RectTransform",
                "x": 583.5560302734375,
                "y": 198.75999450683595,
                "instanceID": 28784,
                "buttons": [],
                "children": []
            }]
        }]
    }, {
        "name": "PopOutMaster (1)",
        "type": "UnityEngine.RectTransform",
        "x": 835.2000122070313,
        "y": 135.52000427246095,
        "instanceID": 28834,
        "buttons": [],
        "children": [{
            "name": "PopOut2",
            "type": "UnityEngine.RectTransform",
            "x": 719.0799560546875,
            "y": 135.52000427246095,
            "instanceID": 28492,
            "buttons": ["PopOut2 (UnityEngine.UI.Button)"],
            "children": [{
                "name": "Text",
                "type": "UnityEngine.RectTransform",
                "x": 617.89599609375,
                "y": 135.52000427246095,
                "instanceID": 28324,
                "buttons": [],
                "children": []
            }]
        }]
    }, {
        "name": "PopOutMaster (2)",
        "type": "UnityEngine.RectTransform",
        "x": 835.2000122070313,
        "y": 72.27999877929688,
        "instanceID": 28716,
        "buttons": [],
        "children": [{
            "name": "PopOut3",
            "type": "UnityEngine.RectTransform",
            "x": 719.0799560546875,
            "y": 72.27999877929688,
            "instanceID": 28800,
            "buttons": ["PopOut3 (UnityEngine.UI.Button)"],
            "children": [{
                "name": "Text",
                "type": "UnityEngine.RectTransform",
                "x": 617.89599609375,
                "y": 72.27999877929688,
                "instanceID": 28442,
                "buttons": [],
                "children": []
            }]
        }]
    }, {
        "name": "BottomFade",
        "type": "UnityEngine.RectTransform",
        "x": 314.79595947265627,
        "y": 12.919998168945313,
        "instanceID": 28592,
        "buttons": [],
        "children": []
    }, {
        "name": "B4",
        "type": "UnityEngine.RectTransform",
        "x": 27.20001220703125,
        "y": 12.919998168945313,
        "instanceID": 28408,
        "buttons": ["B4 (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 27.20001220703125,
            "y": 12.919998168945313,
            "instanceID": 28690,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "ButtonSmall (1)",
        "type": "UnityEngine.RectTransform",
        "x": 81.60000610351563,
        "y": 12.919998168945313,
        "instanceID": 28698,
        "buttons": ["ButtonSmall (1) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 81.60000610351563,
            "y": 12.919998168945313,
            "instanceID": 28484,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "ButtonSmall (2)",
        "type": "UnityEngine.RectTransform",
        "x": 136.0,
        "y": 12.919998168945313,
        "instanceID": 28380,
        "buttons": ["ButtonSmall (2) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 136.0,
            "y": 12.919998168945313,
            "instanceID": 28734,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "ButtonSmall (3)",
        "type": "UnityEngine.RectTransform",
        "x": 190.39999389648438,
        "y": 12.919998168945313,
        "instanceID": 28650,
        "buttons": ["ButtonSmall (3) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 190.39999389648438,
            "y": 12.919998168945313,
            "instanceID": 28568,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "ButtonSmall (4)",
        "type": "UnityEngine.RectTransform",
        "x": 244.8000030517578,
        "y": 12.919998168945313,
        "instanceID": 28556,
        "buttons": ["ButtonSmall (4) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 244.8000030517578,
            "y": 12.919998168945313,
            "instanceID": 28842,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "ButtonSmall (5)",
        "type": "UnityEngine.RectTransform",
        "x": 299.20001220703127,
        "y": 12.919998168945313,
        "instanceID": 28678,
        "buttons": ["ButtonSmall (5) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 299.20001220703127,
            "y": 12.919998168945313,
            "instanceID": 28814,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "ButtonSmall (6)",
        "type": "UnityEngine.RectTransform",
        "x": 353.6000061035156,
        "y": 12.919998168945313,
        "instanceID": 28822,
        "buttons": ["ButtonSmall (6) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 353.6000061035156,
            "y": 12.919998168945313,
            "instanceID": 28670,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "Button (5)",
        "type": "UnityEngine.RectTransform",
        "x": 235.2109375,
        "y": 327.4600830078125,
        "instanceID": 28764,
        "buttons": ["Button (5) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 235.2109375,
            "y": 327.4600830078125,
            "instanceID": 28400,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "Button (6)",
        "type": "UnityEngine.RectTransform",
        "x": 280.5690612792969,
        "y": 327.4600830078125,
        "instanceID": 28752,
        "buttons": ["Button (6) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 280.5690612792969,
            "y": 327.4600830078125,
            "instanceID": 28468,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "Button (7)",
        "type": "UnityEngine.RectTransform",
        "x": 325.9245300292969,
        "y": 327.4600830078125,
        "instanceID": 28430,
        "buttons": ["Button (7) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 325.9245300292969,
            "y": 327.4600830078125,
            "instanceID": 28392,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "Button (8)",
        "type": "UnityEngine.RectTransform",
        "x": 371.2799987792969,
        "y": 327.4600830078125,
        "instanceID": 28360,
        "buttons": ["Button (8) (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 371.2799987792969,
            "y": 327.4600830078125,
            "instanceID": 28532,
            "buttons": [],
            "children": []
        }]
    }, {
        "name": "START",
        "type": "UnityEngine.RectTransform",
        "x": 309.55999755859377,
        "y": 177.0,
        "instanceID": 28350,
        "buttons": ["START (UnityEngine.UI.Button)"],
        "children": [{
            "name": "Text",
            "type": "UnityEngine.RectTransform",
            "x": 309.55999755859377,
            "y": 177.0,
            "instanceID": 28584,
            "buttons": [],
            "children": []
        }]
    }],
    "buttons": ["TopFade (UnityEngine.UI.Button)", "Button (1) (UnityEngine.UI.Button)", "Button (2) (UnityEngine.UI.Button)", "Button (3) (UnityEngine.UI.Button)", "Button (4) (UnityEngine.UI.Button)", "PopOut1 (UnityEngine.UI.Button)", "PopOut2 (UnityEngine.UI.Button)", "PopOut3 (UnityEngine.UI.Button)", "B4 (UnityEngine.UI.Button)", "ButtonSmall (1) (UnityEngine.UI.Button)", "ButtonSmall (2) (UnityEngine.UI.Button)", "ButtonSmall (3) (UnityEngine.UI.Button)", "ButtonSmall (4) (UnityEngine.UI.Button)", "ButtonSmall (5) (UnityEngine.UI.Button)", "ButtonSmall (6) (UnityEngine.UI.Button)", "Button (5) (UnityEngine.UI.Button)", "Button (6) (UnityEngine.UI.Button)", "Button (7) (UnityEngine.UI.Button)", "Button (8) (UnityEngine.UI.Button)", "START (UnityEngine.UI.Button)"]
}
4

1 回答 1

1

现在您已经发布了 JSON 的其余部分,我可以看到您正在尝试访问包装在更大 JSON 对象中的内部数组。

代码本身使用单个“工作”,Value因为您拥有的是单个 JSON 对象,没有数组!

但是,它不会有任何与 JSON 字符串中的条目匹配的字段,因此无论如何它们都会有它们的默认值。


因此,您实际上应该拥有的是包含 JSON 相关字段的包装类

[Serializable]
public class JsonRoot
{
    public string scene;
    public Value[] gameObjectDefinitions;
}

然后反序列化成这种类型

var jsonRoot = JsonConverter.Deserialize<JsonRoot>(req.downloadHandler.text);

现在您可以访问和迭代

foreach(var value in jsonRoot.gameObjectDefinitions)
{
    ...
}

这里的替代方法可能是使用SimpleJson

var jsonRoot = JSON.Parse(req.downloadHandler.text);
var values = jsonRoot["gameObjectDefinitions"].AsArray;

您对以下内容的评论children

您会在Value课堂上发现它们根本不存在,因此目前无法访问它们。

如果你想表示完整的 JSON 格式,你的类应该看起来像

[Serializable]
public class JsonRoot
{
    public string scene;
    public Value[] gameObjectDefinitions;
    public string[] buttons;
}

[Serializable]
public class Value
{
    public string name;
    public string type;
    public float x;
    public float y;
    public int instanceID;

    public string[] buttons;
    public Value[] children;
}

注意:Unity 将此类递归类型结构的序列化限制为最大嵌套级别 7!我不知道您使用的 JSON 库如何处理此问题,但请注意可能存在此限制。

我不知道你的最终目标是什么,但我想迭代所有孩子和子孩子的唯一方法确实是一种递归方法,例如

...

foreach(var value in jsonRoot.gameObjectDefinitions)
{
    HandleValue(value);
}

...

private void HandleValue(Value value)
{
    ...

    if(value.children != null)
    {
        foreach(var child in value.children)
        {
            HandleValue(child);
        }
    }
}
于 2020-03-22T15:57:56.767 回答