简体   繁体   English

无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'Value[]',因为该类型需要 JSON 数组(例如 [1,2,3])

[英]Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Value[]' because the type requires a JSON array (e.g. [1,2,3])

I'm struggling with this exception.我正在为这个例外而苦苦挣扎。 I'm trying to get some info from a json file and get some C# object out of it.我正在尝试从 json 文件中获取一些信息并从中获取一些 C# 对象。 But for some reasons, Unity does not allow me to deserialize it.但出于某些原因,Unity 不允许我反序列化它。

Here's my C# code:这是我的 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);
                }
            }
        }));
    }

I've already googled it and found some people having the same issue but I can't figure out how to fix my problem here.我已经用谷歌搜索了它,发现有些人有同样的问题,但我不知道如何在这里解决我的问题。 I've tried deserializing Value instead of Value[] , and it does work actually.我试过反序列化Value而不是Value[] ,它确实有效。

But then I cannot go through my table with a foreach (which I need)但是后来我不能用 foreach 遍历我的桌子(我需要)

Can anyone help me please ?有人可以帮我吗? Thanks,谢谢,

EDIT : Here's my "Value" types:编辑:这是我的“值”类型:

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

And here's my json file:这是我的 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)"]
}

Now that you have posted the rest of the JSON I can see that you are trying to access an inner array wrapped within a bigger JSON object.现在您已经发布了 JSON 的其余部分,我可以看到您正在尝试访问包含在更大 JSON 对象中的内部数组。

The code itself "worked" with a single Value because what you have there is a single JSON object, no array!代码本身使用单个Value “工作”,因为您拥有的是单个 JSON 对象,没有数组!

However, it wouldn't have any fields matching the entries in the JSON string so they would have their default values anyway.但是,它不会有任何与 JSON 字符串中的条目匹配的字段,因此无论如何它们都会具有默认值。


So what you rather should have is actually that wrapper class that includes the relevant fields of your JSON所以你应该拥有的实际上是包含你的 JSON 相关字段的包装类

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

then Deserialize into this type然后反序列化为这种类型

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

and now you can access and iterate over现在你可以访问和迭代

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

An alternative here might be using SimpleJson这里的另一种选择可能是使用SimpleJson

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

To your comment regarding the children :关于您对children的评论:

You will find in the Value class they are simply non-existent so currently there is no way to access them.您会在Value类中发现它们根本不存在,因此目前无法访问它们。

If you want to represent the complete JSON format your classes should look like如果您想表示完整的 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;
}

Note : Unity limits the serialiazation of such recursive type structures to a maximal nest level of 7!注意:Unity 将此类递归类型结构的序列化限制为最大嵌套级别 7! I don't know how the JSON library you are using handles this but be aware that there might exist this limitation.我不知道您使用的 JSON 库如何处理此问题,但请注意可能存在此限制。

I don't know what your final goal is but I guess the only way to iterate all children and subchildren would indeed be a recursive method like eg我不知道你的最终目标是什么,但我想迭代所有孩子和子孩子的唯一方法确实是一种递归方法,例如

...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将当前JSON数组(例如[1,2,3])反序列化为类型”,因为该类型需要JSON对象(例如{“ name”:“ value”}) - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type '' because the type requires a JSON object (e.g. {“name”:“value”}) 无法将JSON数组(例如[1,2,3])反序列化为类型&#39;&#39;,因为类型需要JSON对象(例如{“ name”:“ value”}) - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) 无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型需要 JSON 数组(例如 [1,2,3] 才能正确反序列化) - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type requires a JSON array (e.g. [1,2,3]) to deserialize correctly 无法将 JSON 数组(例如 [1,2,3])反序列化为类型“ ”,因为类型需要 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})才能正确反序列化 - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly 无法将 JSON 数组(例如 [1,2,3])反序列化为类型“”,因为类型需要 JSON object(例如,将 {“name”} 正确地反序列化为“value”: - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) to deserialize correctly 无法反序列化当前 JSON object 因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化 - Cannot deserialize the current JSON object because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly 如何修复“无法将当前的 JSON 数组反序列化为“段落”类型,因为需要 JSON object(例如 {“name”:“值”) - How to fix 'Cannot deserialize the current JSON array into type 'passages' because requires a JSON object (e.g. {“name”:“value”}) to deserialize 无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“ConsoleAppTest01.Location”,因为该类型需要 JSON ZA8CFDE6331BD59EB2AC96F8911ZC4 - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ConsoleAppTest01.Location' because the type requires a JSON object 无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 无法将当前JSON对象反序列化为类型&#39;WindowsService1 ...需要JSON数组(例如[1,2,3])才能正确反序列化 - Cannot deserialize the current JSON object into type 'WindowsService1…requires a JSON array (e.g. [1,2,3]) to deserialize correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM