How to deserialize JSON with C#

Asked

Viewed 381 times

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 that expression.

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

But you’re making this mistake.

An Exception of type 'Newtonsoft.Json.Jsonserializationexception' occurred in Newtonsoft.Json.DLL but was not handled in user code Additional information: Cannot deserialize the Current JSON array (e.g. [1,2,3]) into type 'Helloworldwp.Mainpage+Rootobject' because the type requires a JSON Object (e.g. {"name":"value"}) to deserialize correctly.

  • 2

    Dude, put up the Exception text and not a print of it.

  • An Exception of type 'Newtonsoft.Json.Jsonserializationexception' occurred in Newtonsoft.Json.DLL but was not handled in user code Additional information: Cannot deserialize the Current JSON array (e.g. [1,2,3]) into type 'Helloworldwp.Mainpage+Rootobject' because the type requires a JSON Object (e.g. {"name":"value"}) to deserialize correctly.

  • 1

    Henry, click [Edit] to edit your question. You can take the image and put the text of Exception, see the answer of @Onosendai, it is correct. By the way, welcome to [en.so]. See the [tour] and visit [help] to better understand how the site works.

1 answer

6


Your JSON is a collection containing only one item:

[{..}]

You should then deserializar it for a collection:

List<RootObject> data = JsonConvert.DeserializeObject<List<RootObject>>(stringdate);
  • First of all thank you for answering. Got it, so this kind of JSON is a collection. I put this expression you told me Jsonconvert.Deserializeobject<List<Rootobject>(stringdate); continues giving the error, but now neither compiles. I don’t know if you’re missing any import. Because I already put and installed that newtonsoft.json

  • 1

    He’s a collection when he has the clasps [...] @Henriquealbuquerque

  • Cannot implicitly convert type 'System.Collections.Generic.List<HelloWorldWP.MainPage.RootObject>' to 'HelloWorldWP.MainPage.RootObject'.

  • got it, I’ll be on the next time json types @jbueno

  • @Henriquealbuquerque sorry, my example contained an error. I corrected the last line.

  • @Onosendai I apologize for disturbing haha, sorry, now that I understand that my JSON is a list, at the time of assigning it is not finding the as_name, for example. data.as_name.Tostring();... I think something is missing between the date variable and as_name, thus becoming date.??????. as_name.Tostring();

  • @Henriquealbuquerque does not stress, it is a pleasure to help. = ) Its variable data now is a collection: Try, for example, data[0].as_nome.ToString(); where zero is the index of the object within the collection.

  • @Onosendai GG! ran nicely, thanks! I’m starting with C# and with webservices.

  • @Henriquealbuque cool to read this! These are interesting technologies. Feel free to post more questions in case of any questions.

  • @Henriquealbuquerque, Jsonconvert is part of which Namespace, I did not find here the ddl nor to reference.

  • @Marconciliosouza vc has to go to nuget Packages and proccure by json.net, after installing... using newtonsoft.json.. that works there.

  • 1

    @MarconcilioSouza https://www.nuget.org/packages/Newtonsoft.Json/

  • @Onosendai , thank you!

Show 8 more comments

Browser other questions tagged

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