How to deserialize JSON with C #

2

I have this JSON

[{
"new_as_cod": "0010955",
"as_nome": "NAME",
"as_cpf": "1212121212",
"as_email": "[email protected]",
"as_cep": "88.025-200",
"igr_nome": "1\u00aa IGREJA BATISTA - FLORIANOPOLIS",
"id": "2781",
"valor": "50.00",
"pg_tipo_id": "CC",
"status": "Ativo",
"idstatus": "1"
}]

class was generated from from here

 public class RootObject
{
    public string new_as_cod { get; set; }
    public string as_nome { get; set; }
    public string as_cpf { get; set; }
    public string as_email { get; set; }
    public string as_cep { get; set; }
    public string igr_nome { get; set; }
    public string id { get; set; }
    public string valor { get; set; }
    public string pg_tipo_id { get; set; }
    public string status { get; set; }
    public string idstatus { get; set; }
}

I've been trying to use this expression.

RootObject data = JsonConvert.DeserializeObject<RootObject>(stringdate);
as_nome.Text = data.as_nome.ToString();
as_cpf.Text = data.as_cpf.ToString();

But you're giving this error.

  An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.DLL but was not handled in user code Additional information:   Can not deserialize the current JSON array (eg [1,2,3]) into type 'HelloWorldWP.MainPage + RootObject' because the type requires a JSON object (e.g. {"name": "value"}) to deserialize correctly.

    
asked by anonymous 28.10.2015 / 14:14

1 answer

6

Your JSON is a collection containing one item only:

[{..}]

You should then deserialize it to a collection:

List<RootObject> data = JsonConvert.DeserializeObject<List<RootObject>>(stringdate);
    
28.10.2015 / 14:17