如何反序列化这个嵌套的 json 响应并用多个对象保存数组值(在 Unity 中)

How to deserialise this nested json response and save the the array values with multiple Jobjects (in Unity)

我在 JSON 方面几乎没有经验,而且我遇到了一个问题。感谢任何帮助。

我想从 additionalInformation 数组中专门访问名称的值。

JSON 响应:

    {
    "statusCode": 200,
    "version": 1,
    "jsonData": [
        {
            "additionalInformation": [
                {
                    "id": "XXX94XXXX9xxXx_xxxXXXX",
                    "name": "xxxx xxx x xxxxxxxx"
                },
                {
                    "id": "0xXXxcXxv5PQqTi2zLgV",
                    "name": "xxx xxxxxxxx"
                },
                {
                    "id": "11Krt_our2rPCPqJ_2fKZR",
                    "name": "xxx xxxxxxxx xx"
                },
                {
                    "id": "2jYw4IyBP8KuozM_ej7DGf",
                    "name": "xxxxxxx 1"
                },
                {
                    "id": "3B8O805wL1ufabHMz1Je3v",
                    "name": "xxxxxxx 2"
                },
                {
                    "id": "0FVKUYZkvFaxd_OQUiyPBZ",
                    "name": "xxxxxxx"
                },
                {
                    "id": "3O41QFd0573QQvFco5zUUP",
                    "name": "Xxxxxxxxx"
                }
            ],
            "type": 0
        }
    ],
    "errorMessages": [],
    "warningMessages": [],
    "informationMessages": []
}

型号:

public class CFunctions
{
    public int statusCode { get; set; }
    public int version { get; set; }

    public List<PFunctions>[] jsonData { get; set; }

    public List<string> errorMessages { get; set; }

    public List<string> warningMessages { get; set; }

    public List<string> informationMessages { get; set; }

    /*public CFunctions()
    {
        jsonData = new List<PFunctions>();
    }*/
   }
[Serializable]
public class PFunctions 
{
    public List<PAdditionalInfo>[] additionalInformation { get; set; }

    public int type { get; set; }

    /*public PFunctions()
    {
        additionalInformation = new List<PAdditionalInfo>();
    }*/
}
[Serializable]
public class PAdditionalInfo 
{
   public  Guid id { get; set; }

   public string name { get; set; }
}

反序列化

var request = UnityWebRequest.Get(baseurl);
var operation = request.SendWebRequest();
var jsonResponse = request.downloadHandler.text;
List<CFunctions>[] PFunctionsList = JsonConvert.DeserializeObject<List<CFunctions>[]>(jsonResponse);

错误:

Cannot deserialize the current JSON object into type 'System.Collections.Generic.List`1[CFunctions][]' because the type requires a JSON array to deserialize correctly.
To fix this error either change the JSON to a JSON array or change the deserialized type so that it is a normal .NET type that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'statusCode', line 1, position 14. UnityEngine.Debug:Log(Object)

我试过的

即使我更改了 List<PAdditionalInfo> to List<PAdditionalInfo>[]

,错误仍然存​​在

我不确定如何使用 JsonObjectAttribute 以及它是否是最好的方法。

您已经在模型中声明了一个 List<T> 的数组,例如 List<PAdditionalInfo>[]。 json 表示单个数组,不嵌套。您可以通过选择一个或另一个来解决这个问题(我决定使用 List<> 但数组也有效):

public class PFunctions 
{
    public List<PAdditionalInfo> additionalInformation { get; set; }  // removed []
    ...
}

public class CFunctions
{
    public int statusCode { get; set; }
    public int version { get; set; }

    public List<PFunctions> jsonData { get; set; }    // removed []
    ...
}

您要反序列化的 class 不正确。反序列化为正确的类型(CFunctions 而不是 List<CFunctions>[]):

CFunctions cFunctions = JsonConvert.DeserializeObject<CFunctions>(json);

获取附加信息的最有效方法是这一行代码,您只需要一个 class

 List<AdditionalInformation>  additionalInformation = JObject.Parse(json)
["jsonData"][0]["additionalInformation"].ToObject<List<AdditionalInformation>>();

class

public class AdditionalInformation
    {
        public string id { get; set; }
        public string name { get; set; }
    }