Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I have little to no experience in JSON and I am stuck with a problem. Any help is appreciated.

I want to access specifically the names’ values from the additionalInformation array.

JSON Response:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    {
    "statusCode": 200,
    "version": 1,
    "jsonData": [
        {
            "additionalInformation": [
                {
                    "id": "XXX94XXXX9xxXx_xxxXXXX",
                    "name": "xxxx xxx x xxxxxxxx"
                },
                {
                    "id": "0xXXxcXxv5PQqT$6i2zLgV",
                    "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": []
}

Model:

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

Deserialisation

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

Error:

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)

What I tried

The error pertains even when I changed List<PAdditionalInfo> to List<PAdditionalInfo>[]

I am not sure how to use JsonObjectAttribute and if it is the best way.

>Solution :

You’ve declared an array of List<T> in the models, eg List<PAdditionalInfo>[]. The json represents single arrays, not nested. You can fix that by choosing one or the other (I decided to use List<> but array is valid too):

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 []
    ...
}

The class you’re deserializing to is incorrect. Deserialize to the correct type (which is CFunctions not List<CFunctions>[]):

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading