Unable to parse JSON data with dictionary list

advertisements

I want to parse JSON Data into C#

The data is:

"{\"data\":

{\"6\":{\"id\":\"6\",\"reward_definition_id\":12,\"person_id\":164305,\"issue_date\":\"2015-10-08\",\"expiry_date\":\"2015-11-08\",\"amount_initial\":20,\"amount_remaining\":0,\"

_member_details\":{\"id\":\"164305\",\"first_name\":\"test\",\"last_name\":\"one\",\"email\":\"[email protected]\",\"_email_trigger\":\"Y\",\"_email_bounced\":\"N\",\"_email_promo\":\"Y\",\"_app_notify\":\"Y\",\"_sms_status\":\"Y\",\"active\":\"Y\"},

\"_store_details\":null,\"_staff_details\":null,\"_product_details\":null},

\"5\":{\"id\":\"5\",\"reward_definition_id\":12,\"person_id\":164305,\"issue_date\":\"2015-10-08\",\"expiry_date\":\"2015-11-08\",\"amount_initial\":20,\"amount_remaining\":20,\"

_member_details\":{\"id\":\"164305\",\"first_name\":\"test\",\"last_name\":\"one\",\"email\":\"[email protected]\",\"_email_trigger\":\"Y\",\"_email_bounced\":\"N\",\"_email_promo\":\"Y\",\"_app_notify\":\"Y\",\"_sms_status\":\"Y\",\"active\":\"Y\"},

\"_store_details\":null,\"_staff_details\":null,\"_product_details\":null}

}}"

How to create a Class with Dictionary of lists of this data and deserialize it into C# class. I am trying to use the following classes to store this json data, the classes are

public class JsonRewardResponse
{
    public Dictionary<int, JsonRewardDetails> mydictionary { get; set; }
}

public class JsonRewardDetails
{
    public string Id;
    public int reward_definition_id;
    public int person_id;
    public string issue_date;
    public string expiry_date;
    public int amount_initial;
    public int amount_remaining;
    public JsonMemberDetails member;`enter code here`
    public int _store_details;
    public int _staff_details;
    public int product_details;
}

public class JsonMemberDetails
{
    public string Id;
    public string first_name;
    public string last_name;
    public string email;
    public string _email_trigger;
    public string _email_bounced;
    public string _email_promo;
    public string _app_notify;
    public string _sms_status;
    public string active;
}

Please help Thanks in Advance.

Bharath AK


You don't say what serializer you are using, so the advice I am giving is relevant for for all JSON serializers.

You have a few problems here:

  1. The name of the property mydictionary should be data, as that's what is in the JSON.

  2. product_details should be renamed to _product_details.

  3. _store_details, _staff_details and _product_details are declared to be int, however they appear as null in the JSON. Thus they need to be some sort of nullable type in c#, for instance int? or string. (The fact that these are null in the JSON suggests, but does not prove, that they are not integer types when non-null.)

  4. member should be renamed to _member_details.

Thus:

public class JsonRewardResponse
{
    public Dictionary<int, JsonRewardDetails> data { get; set; }
}

public class JsonRewardDetails
{
    public string Id;
    public int reward_definition_id;
    public int person_id;
    public string issue_date;
    public string expiry_date;
    public int amount_initial;
    public int amount_remaining;
    public JsonMemberDetails member;
    public int? _store_details;
    public int? _staff_details;
    public int? _product_details;
}

public class JsonMemberDetails
{
    public string Id;
    public string first_name;
    public string last_name;
    public string email;
    public string _email_trigger;
    public string _email_bounced;
    public string _email_promo;
    public string _app_notify;
    public string _sms_status;
    public string active;
}

With these fixes, using Json.NET, you can do:

var response = JsonConvert.DeserializeObject<JsonRewardResponse>(json);

(You might have additional issues with other serializers.)