1

Let me explain my problem. So I have JSON:

    {"num":20, "meta":[{"id":312, "identif":{"type":true,"status":false}}}]}

I am currently grabbing the meta id field with:

    var id = JsonConvert.DeserializeObject<typeObj>
    (returnJSON(ApiUrl)).meta[0].id;

class to refrence:

    class typeObj
    {
        public int num {get; set; }
        public List<metatypes> meta {get; set;}
    }
    class metatypes
    {
        public int id {get; set;}
    }

The issue doesn't lay here though. I am trying to get the indentif status element from meta.

I have tried putting a list in metatypes like:

    class metatypes
    {
        public int id {get; set;}
        public List<idtypes> identif {get; set;}
    }
    class idtypes
    {
        public bool type {get; set;}
        public bool status {get; set;}
    }

Calling it with:

    var id = JsonConvert.DeserializeObject<typeObj>
    (returnJSON(ApiUrl)).meta[0].identif[0].status;

But when I try this it returns

'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1'

Looked around and couldn't find a direct solution to my problem.

1 Answer 1

1

You have an incorrect json for the desired structure:

Given classes:

class typeObj
{
    public int num {get; set; }
    public List<metatypes> meta {get; set;}
}

class metatypes
{
    public int id {get; set;}
    public List<idtypes> identif {get; set;}
}
class idtypes
{
    public bool type {get; set;}
    public bool status {get; set;}
}

Your json should look like (identif must be an array): (.NET Fiddle)

{"num":20, "meta":[{"id":312, "identif":[{"type":true,"status":false}]}]}

For the json in question your classes should be like this: (.NET Fiddle)

class typeObj
{
    public int num {get; set; }
    public List<metatypes> meta {get; set;}
}

class metatypes
{
    public int id {get; set;}
    public idtypes identif {get; set;}
}
class idtypes
{
    public bool type {get; set;}
    public bool status {get; set;}
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are a life saver! Thank you! Can't believe that is all it took!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.