Gustavo Bocalandro
Gustavo Bocalandro

Reputation: 79

How do I use data I receive from API in JSON format?

After I called an API (get) I received this answer:

"medios": [
    {
        "id": "2",
        "name": "Banco Galicia",
        "type": "a",
        "img": "/method/21030342",
        "medios_id": "0"
    },
    {
        "id": "3",
        "name": "Bango River",
        "type": "Banco",
        "img": "/method/210321012",
        "medios_id": "0"
    }
]

And I have my "get / set" class:

public class Medio
    {
        public string id { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public string img { get; set; }
        public string medios_id { get; set; }
    }

My question is: how do I make them work together... meaning: have id with the ids I receive from the api, and so on?

Thanks!

Upvotes: 0

Views: 65

Answers (2)

Amit Soni
Amit Soni

Reputation: 3316

Try as follow:-

var yourObj = JsonConvert.DeserializeObject<List<Medio>>(json);

Upvotes: 1

Kayani
Kayani

Reputation: 972

Just use the NewtonSoft.Json package from NuGet to convert the json to your class type list

List<Medio> list = JsonConvert.DeserializeObject<List<Medio>>(your_string)

Upvotes: 1

Related Questions