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

Posted

技术标签:

【中文标题】无法将当前 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])无法将当前 JSON 对象(例如 "name":"value")反序列化为类型“Value[]”,因为该类型需要 JSON 数组(例如 [1,2,3]) 【发布时间】:2020-07-03 02:32:57 【问题描述】:

我正在努力解决这个异常。 我正在尝试从 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)"]

【问题讨论】:

显示的 JSON 是单个对象。 没有foreach 的“表”。你能解释一下为什么你希望能够做到这一点吗?在反序列化步骤后将单个 Value 实例 放入 数组/列表有帮助吗?为什么你“需要”foreach 处理单个对象? (但是是的:反序列化为 Value 在这里看起来是正确的) 能否显示req.downloadHandler.text返回的json 嗯,我的Json文件里其实有很多项目。所以我需要遍历所有这些并选择正确的一个。这就是为什么我需要foreach。我已经发布了 json 文件作为答案。所以基本上,我有 20 件这样的物品。我想在所有其他项目中挑选这个特定项目。 你能显示你的Value类型吗?另请在您的问题中包含收到的 JSON 内容。 【参考方案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);
        
    

【讨论】:

该死,很抱歉让你烦恼,但我被困在这里了。您的解决方案运行良好,但我在尝试访问 PopOut1、PopOut2 和 PopOut3 时仍然遇到问题。这些项目是 PopOutMaster、PopOutMaster (1) 和 PopOutMaster (2) 的子项。因此,当我通过我的 foreach 循环时,我可以找到 PopOutMaster,但我无法找到访问其子项的方法。知道怎么做吗? 见底部更新

以上是关于无法将当前 JSON 对象(例如 "name":"value")反序列化为类型“Value[]”,因为该类型需要 JSON 数组(例如 [1,2,3])的主要内容,如果未能解决你的问题,请参考以下文章

无法将当前 JSON 对象(例如 "name":"value")反序列化为类型 'System.Collections.Generic.List`1

JSON.Net - 无法将当前 json 对象(例如 "name":"value")反序列化为类型 'system.collections.generic.l

Blazor WASM“无法反序列化当前 JSON 对象(例如 "name":"value")”错误

无法将 JSON 数组(例如 [1,2,3])反序列化为类型 ' ',因为类型需要 JSON 对象(例如 "name":"value")才能正确反序列化

无法将 JSON 数组(例如 [1,2,3])反序列化为类型 ' ',因为类型需要 JSON 对象(例如 "name":"value")才能正确反序列化

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