C # - Decode array JSON

0

I need to get the id of this code JSON using C# :

  

[{"nome":"Gabriel Ferreira","cidade":"São Paulo","uf":"SP","id":"4274892"}]

Someone can advise me how I can do this in the best way, since I already tried with JSON.net as follows:

public class pegarID    
{
    public string nome { get; set; }
    public string cidade { get; set; }
    public string uf { get; set; }
    public string id { get; set; }
}
pegarID pegarid = JsonConvert.DeserializeObject<pegarID>(responseString);
Console.WriteLine(pegarid.id);

But it did not work ...

    
asked by anonymous 06.05.2018 / 05:22

1 answer

1

The JSON that you are trying to convert is a coleção , not a single object. (Note the brackets "[" and "] in the text)

Try

public class pegarID
{
    public string nome { get; set; }
    public string cidade { get; set; }
    public string uf { get; set; }
    public string id { get; set; }
}
pegarID[] colecaoPegarid = JsonConvert.DeserializeObject<pegarID[]>(responseString);

Console.WriteLine(colecaoPegarid[0].id);

Source: link

    
06.05.2018 / 06:54