3

I am getting Json data from a web server, but when I try to deserialize it to objects, I am not getting any data. The Json string looks like this:

{"success":true,"data":[{"Id":6,"CustomerGuid":"70b390d8-82d5-4bba-aa68-fc8268a1b1ff","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":false,"Active":false,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":null,"CreatedOnUtc":"\/Date(1472933472393)\/","LastLoginDateUtc":null,"LastActivityDateUtc":"\/Date(1472933472393)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[]},{"Id":5,"CustomerGuid":"eb9e6f24-f362-4c10-942a-366e2919dc11","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":false,"Active":false,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":null,"CreatedOnUtc":"\/Date(1472933472363)\/","LastLoginDateUtc":null,"LastActivityDateUtc":"\/Date(1472933472363)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[]},{"Id":4,"CustomerGuid":"9f46dbae-6942-410c-90b8-9b38a0890064","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":false,"Active":false,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":null,"CreatedOnUtc":"\/Date(1472933472317)\/","LastLoginDateUtc":null,"LastActivityDateUtc":"\/Date(1472933472317)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[]},{"Id":3,"CustomerGuid":"6277386b-13ee-427b-9cfe-4ebfa487c340","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":false,"Active":false,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":null,"CreatedOnUtc":"\/Date(1472933472253)\/","LastLoginDateUtc":null,"LastActivityDateUtc":"\/Date(1472933472253)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[]},{"Id":2,"CustomerGuid":"241f45f1-b38c-4e22-8c5a-743fa3276620","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":false,"Active":false,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":null,"CreatedOnUtc":"\/Date(1472933472207)\/","LastLoginDateUtc":null,"LastActivityDateUtc":"\/Date(1472933472207)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[]},{"Id":1,"CustomerGuid":"a940dc03-5f52-47d2-9391-8597b3b31cf2","UserName":"[email protected]","Email":"[email protected]","CustomerRoles":[{"Id":1,"Name":"Administrators","SystemName":"Administrators"},{"Id":2,"Name":"Forum Moderators","SystemName":"ForumModerators"},{"Id":3,"Name":"Registered","SystemName":"Registered"}],"AdminComment":null,"IsTaxExempt":false,"AffiliateId":0,"VendorId":0,"HasShoppingCartItems":true,"Active":true,"Deleted":false,"IsSystemAccount":false,"SystemName":null,"LastIpAddress":"71.185.255.7","CreatedOnUtc":"\/Date(1472933470783)\/","LastLoginDateUtc":"\/Date(1477522483903)\/","LastActivityDateUtc":"\/Date(1477523996553)\/","ExternalAuthenticationRecords":[],"ShoppingCartItems":[{"Id":1,"StoreId":1,"ShoppingCartTypeId":1,"CustomerId":1,"ProductId":18,"AttributesXml":null,"CustomerEnteredPrice":0.0000,"Quantity":1,"CreatedOnUtc":"\/Date(1473801903447)\/","UpdatedOnUtc":"\/Date(1473803336207)\/","IsFreeShipping":false,"IsShipEnabled":true,"AdditionalShippingCharge":0.0000,"IsTaxExempt":false}]}]}

I created these classes from the recommendation given in this link: recommendation

I used this to create the classes: json2csharp

Response class:

class Response
{
    bool success;
    IList<Customer> data;
}

Customer class:

class Customer
{
    public int Id { get; set; }
    public string CustomerGuid { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public List<CustomerRole> CustomerRoles { get; set; }
    public object AdminComment { get; set; }
    public bool IsTaxExempt { get; set; }
    public int AffiliateId { get; set; }
    public int VendorId { get; set; }
    public bool HasShoppingCartItems { get; set; }
    public bool Active { get; set; }
    public bool Deleted { get; set; }
    public bool IsSystemAccount { get; set; }
    public object SystemName { get; set; }
    public string LastIpAddress { get; set; }
    public DateTime CreatedOnUtc { get; set; }
    public DateTime? LastLoginDateUtc { get; set; }
    public DateTime LastActivityDateUtc { get; set; }
    public List<object> ExternalAuthenticationRecords { get; set; }
    public List<object> ShoppingCartItems { get; set; }

}

CustomerRole class:

class CustomerRole
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string SystemName { get; set; }

}

ExternalAuthenticationRecord class:

class ExternalAuthenticationRecord
{
    public int Id { get; set; }
    public int CustomerId { get; set; }
    public string Email { get; set; }
    public object ExternalIdentifier { get; set; }
    public object ExternalDisplayIdentifier { get; set; }
    public object OAuthToken { get; set; }
    public object OAuthAccessToken { get; set; }
    public string ProviderSystemName { get; set; }
}

ShoppingCartItem class:

class ShoppingCartItem
{
    public int Id { get; set; }
    public int StoreId { get; set; }
    public int ShoppingCartTypeId { get; set; }
    public int CustomerId { get; set; }
    public int ProductId { get; set; }
    public object AttributesXml { get; set; }
    public double CustomerEnteredPrice { get; set; }
    public int Quantity { get; set; }
    public DateTime CreatedOnUtc { get; set; }
    public DateTime UpdatedOnUtc { get; set; }
    public bool IsFreeShipping { get; set; }
    public bool IsShipEnabled { get; set; }
    public double AdditionalShippingCharge { get; set; }
    public bool IsTaxExempt { get; set; }

}

I am using this statement to deserialzie the Json string: Response res = (Response)JsonConvert.DeserializeObject(customerJson, (typeof(Response)));

When I stop it in the debugger, it shows "res" as data: null and success: false.

I am not getting any errors. It is just not giving me the data from the Json string.

Any help that anybody can provide to figure out why I'm not getting the data I want in "res", would be gratefully appreciated.

Thanks, Tony

3
  • 1
    Please take some time to revise your question into a minimal reproducible example - put effort into formatting both the JSON and all the code, make it minimal, and complete so that we can reproduce the problem. Commented Nov 1, 2016 at 17:02
  • 2
    It looks like it should be Response res = JsonConvert.DeserializeObject<Response>(customerJson); Commented Nov 1, 2016 at 17:06
  • @Equalsk - I tried your method and I get the same results. I used this as a guide to create my statement: [newtonsoft.com/json/help/html/DeserializeObject.htm]. Thanks. Commented Nov 1, 2016 at 17:13

1 Answer 1

4

The problem is related to the accessibility level in your Response class. By default the fields, property and method are private so JsonConvert is not able to fill the properties.
Change the class as follow:

class Response
{
    public bool success {get; set;}
    public IList<Customer> data {get; set;}
}

And it wil works.
Another improvement is related to the JsonConvert use. To avoid the explicit cast use this type conversion: JsonConvert.DeserializeObject<T>(string) where T will be Response

Sign up to request clarification or add additional context in comments.

2 Comments

I tried what you recommend and I get the same results. data:null and success:false. Thanks for your help.
OOPS! Sorry, I forgot to make the properties "public". It does work as you recommended. I am now getting data from the Json string. Thanks for your help.

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.