Jump to content

Deserializing


 Share

Recommended Posts

Hi,

I'm trying to retrieve a list of customer contacts and, for each one, perform some crud processes on another system; this is using c#. I used the api response sample for Get all contacts to copy the resulting json string and used the paste special .. paste as json classes feature of Visual Studio, however when I run my code the Deserialization function doesn't return any data, although the raw json string has returned exactly what I expected. I'm thinking the classes have not been generated correctly for some reason. Here's the first bit of the classes that get generated.

{
public class SageOneContactRootobject
{
    public SageOneContact[] Property1 { get; set; }
}

public class SageOneContact
{
    public string id { get; set; }
    public string displayed_as { get; set; }
    public string path { get; set; }
    public DateTime created_at { get; set; }
    public DateTime updated_at { get; set; }
    public Link[] links { get; set; }
    public DateTime deleted_at { get; set; }
    public int balance { get; set; }

and on it goes. I try and Deserialize as follows:

                    string contacturl = "https://api.accounting.sage.com/v3.1/contacts/?attributes=all&contact_type_id=CUSTOMER";
                    var contactresponse = client.GetAsync(contacturl);
                    var contactcontent = await contactresponse.Result.Content.ReadAsStringAsync();
                    SageOneContactRootobject SageOneContactRootobject = JsonConvert.DeserializeObject<SageOneContactRootobject>(contactcontent);

Am I doing something daft?

Cheers

Reg

 

Link to comment
Share on other sites

  • Administrators

Hi Reginald,

So is the JSON response held in the variable 'contactcontent' correct? 

Ideally if we could see the value of that variable, to verify the response has been successful that would be a good start. Also as far as I can tell from that, you would need to have defined getters and setters for all of the properties you wish to have available on the SageOneContactRootobject object? It appears in your class definition for SageOneContactRootobject you aren't? 

If thats correct then you should just be able to use the values of the properties on the newly populated object (SageOneContactRootobject.id etc).

Link to comment
Share on other sites

Hi.  I thought I'd probably not set up class definitions up correctly, so I used Postman to give me a sample output of all contacts, then found a great website  (Convert JSON to C# Classes Online - Json2CSharp Toolkit) that takes a pasted json message and generates the required class structure., and this solved it.

Thanks for responding.

Reg

 

  • Like 2
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...