PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 6, 2022

[FIXED] How to format JSON data as C# object where object properties are generated dynamically

 September 06, 2022     c#, json, json-deserialization, mailchimp, mailchimp-api-v3.0     No comments   

Issue

I'm not sure if my subject is describing my question correctly but hopefully it is.

I'm currently working on a project that requires integration with Mailchimp and one of the Member's object property is called the "merge_fields" which returns an array of key value pair.

The Json data (partial only for simplicity sake) is returned as follows:

"members": [{
    "id": "9ff9398a92599aca00401da0fb9f0339da",
"email_address": "myemail@mydomain.com",
"unique_email_id": "90ds8927dae",
"email_type": "html",
"status": "subscribed",
"merge_fields": {
    "FNAME": "My FirstName",
        "LNAME": "My LastName",
        "ADDRESS": "My Address",
        "PHONE": "+555 1234 5678",
        "MMERGE5": ""
},
"stats": {
"avg_open_rate": 0,
"avg_click_rate": 0
},
"ip_signup": "",
"timestamp_signup": "",
...

As you can see the "merge_fields" property is an array of key pair values where the key is set dynamically and so is the value but I'm not sure on how to define this in .NET so that it can be deserialized correctly using .NET NewtonSoft.

In my Member class, the MergeFields property is defined as follows:

[JsonProperty("merge_fields")]
public MailchimpMergeFields MergeFields { get; set; }

and my MailchimpMergeFields class is defined as follows:

public class MailchimpMergeFields
{
    /// <summary>
    /// An individual merge var and value for a member.
    /// </summary>
    [JsonProperty("merge_fields")]
    public List<KeyValuePair<object, object>> MergeFields { get; set; }

    public MailchimpMergeFields()
    {
        MergeFields = new List<KeyValuePair<object, object>>();
    }
}

The problem is that when the data is deserialized, it returns 0 element in array of the merge_fields property.

Any idea how I deserialize:

"merge_fields": {
    "FNAME": "My FirstName",
    "LNAME": "My LastName",
    "ADDRESS": "My Address",
    "PHONE": "+555 1234 5678",
    "MMERGE5": ""
},

to a list of KeyValuePair (or KeyValuePair) and store this in a .NET class

Thanks.


Solution

This json pattern is easily converted to a dictionary

[JsonProperty("merge_fields")]
public Dictionary<string, string> MergeFields { get; set; }


Answered By - TheGeneral
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing