C# - Decode JSON array

Asked

Viewed 239 times

0

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

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

Can someone advise me how I can do this in the best way, because I’ve 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 didn’t work...

  • 1

    Has there been an exception? If so, what was it? If not, what didn’t work? The property came empty?

1 answer

1


The JSON 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);

inserir a descrição da imagem aqui Source: http://json.org/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.