0

I am trying to deserialize a JSON array into a list of objects. I know how to normally do this but the JSON is returning in a format I don't know how to with.


{
    "result": {
        "account": [
            {
                "id": "18192504834",
                "ownerID": null,
                "accountName": "Citrix",
                "industry": null,
                "phone": "",
                "annualRevenue": null,
                "numberOfEmployees": "0",
                "website": "",
                "yearStarted": null,
                "fax": null,
                "billingCity": null,
                "billingCountry": null,
                "billingPostalCode": null,
                "billingState": null,
                "billingStreetAddress": null,
                "shippingCity": null,
                "shippingCountry": null,
                "shippingPostalCode": null,
                "shippingState": null,
                "shippingStreetAddress": null,
                "account_status_634798fb3a786": null,
                "company_linkedin_url_6347990655c57": null,
                "focus_area___primary_634799ab2f32f": null,
                "normalised___industry_634799cabc901": null,
                "normalised___number_of_employees_63479a161a8e2": null,
                "target_markets_63479a4007cd9": null,
                "focus_area___secondary_63479a64b0d21": null,
                "normalised___address_1_63479aa361d84": null,
                "normalised___address_2_63479ac2a3ede": null,
                "normalised___town__city_63479acf156f5": null,
                "normalised___state_63479b13aa948": null,
                "normalised___post_code_63479b66a1b77": null,
                "normalised___country_63479c84d2e4d": null,
                "normalised___region_63479cb3129f9": null,
                "bulk_import_list_source_63479cd899e77": null,
                "cwid_635887724a213": null
            },

Here is the class I am trying to convert it into

    public class Account{
        public string id { get; set; }
        public string ownerID { get; set; }
        public string AccountName { get; set; }
        public string Industry { get; set; }
        public string Phone { get; set; }
        public string AnnualRevenue { get; set; }
        public string NumberOfEmployees { get; set; }
        public string Website { get; set; }
        public string YearStarted { get; set; }
        public string Fax { get; set; }
        public string BillingCity { get; set; }
        public string BillingCountry { get; set; }
        public string BillingPostalCode { get; set; }
        public string BillingState { get; set; }
        public string BillingStreetAddress { get; set; }
        public string ShippingCity { get; set; }
        public string ShippingCountry { get; set; }
        public string ShippingPostalCode { get; set; }
        public string ShippingState { get; set; }
        public string ShippingStreetAddress { get; set; }
        public string AccountStatus { get; set; }
        public string CompanyLinkedinURL { get; set; }
        public string FocusArea { get; set; }
        public string NormalisedIndustry { get; set; }
        public string NormalisedNumberOfEmployees { get; set; }
        public string TargetMarkets { get; set; }
        public string FocusAreaSecondary { get; set; }
        public string NormalisedAddress { get; set; }
        public string NormalisedAddressTwo { get; set; }
        public string NormalisedTownCity { get; set; }
        public string NormalisedState { get; set; }
        public string NormalisedPostCode { get; set; }
        public string NormalisedCountry { get; set; }
        public string NormalisedRegion { get; set; }
        public string BulkImportList { get; set; }
        public string CWID { get; set; } 
    }

Here is how I am retrieving the JSON and attempting to deserialize it

var request2 = new RestRequest()
{
    Method = Method.Post,
    Timeout = 300000
};

var SharpSpringClient = new RestClient("https://api.sharpspring.com/pubapi/v1.2/?accountID=&secretKey=");
var json = "{\"method\":\"getAccounts\",\"id\":\"zapier_getaccount\",\"params\":{\"where\":{}}}";
request2.AddStringBody(json, ContentType.Json);
RestResponse response2 = SharpSpringClient.Execute(request2);
var result2 = JsonConvert.DeserializeObject<List<Account>>(response2.Content);

This then fails with Cannot deserialize the current JSON object..... I know it's failing because a list of Accounts doesn't account for the result and account json objects.

I also tried the answer from this question Not able to deserialize JSON array into C# list and made these classes

    public class RootObject
    {
        [JsonProperty("result")]
        public string result { get; set; }
        public RootAccount rootAccount { get; set; }
    }
    public class RootAccount
    {
        [JsonProperty("account")]
        public string account { get; set; }
        public List<Account> accounts { get; set; }
    }

But it then fails with this enter image description here

1
  • it looks like you are trying to jam an array into a string. Commented Nov 16, 2022 at 2:01

1 Answer 1

2

Your class structure should look something like this

public class Result
{
    public List<Account> account { get; set; }
}

public class RootObject
{
    public Result result { get; set; }
}

For deserializing:

var result2 = JsonConvert.DeserializeObject<RootObject>(response2.Content);
Sign up to request clarification or add additional context in comments.

3 Comments

This fixes the error but the result object is null
In addition to this, you need to change your deserialization to ``` var result2 = JsonConvert.DeserializeObject<RootObject>(response2.Content); ```
Yeah I did and am getting a null for the result object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.