0

I am new to C#. I have successfully consumed the Mailchip API with a simple console app. I am pulling in relevant data and I have successfully used a JSON deserializer on the returned data (Newtonsoft.Json via NuGet). What I need to find is the best way to place the data into variables which I can then pass to a stored procedure to save the data. I know SQL and I know how to pass variables to a stored procedure in C# (connection strings and such).

What I don't know is how to parse through the deserialized data and then place each value into a variable or something I can pass to SQL. I know this is a vague question so here is the current code.

Class MailChimpAPIClient.cs:

  //Declare the API Key
    public const string APIKey = "My API Key Here";



    public static void APIGet(string url)
    {

        //Syncronious Consumption
        var syncClient = new WebClient();
        syncClient.Headers.Add(APIKey);
        var content = syncClient.DownloadString(url);
        //Deseralize the Json String
        MailChimpData data = JsonConvert.DeserializeObject<MailChimpData>(content);


    }

Class: MailChimpData.cs:

 [DataContract]

public class MailChimpData {

    [DataMember]
    public List<Unsubscribes> unsubscribes { get; set; }
    [DataMember]
    public string campaign_id { get; set; }
    [DataMember]
    public List<Link2> _links { get; set; }
    [DataMember]
    public int total_items { get; set; }
    [DataMember]
    public Link link { get; set; }
    [DataMember]
    public MergeFields mergefields { get; set; }
    [DataMember]
    public Stats stats { get; set; }
    [DataMember]
    public Location location { get; set; }
    [DataMember]
    public List<Member> members { get; set; }
    [DataMember]
    public string list_id { get; set; }

}


//Unsubscribe Data
public class Link
{
    public string rel { get; set; }
    public string href { get; set; }
    public string method { get; set; }
    public string targetSchema { get; set; }
    public string schema { get; set; }
}

public class Unsubscribes
{

    public string email_id { get; set; }
    public string email_address { get; set; }
    public string timestamp { get; set; }
    public string reason { get; set; }
    public string campaign_id { get; set; }
    public string list_id { get; set; }
    public List<Link> _links { get; set; }
}

public class Link2
{
    public string rel { get; set; }
    public string href { get; set; }
    public string method { get; set; }
    public string targetSchema { get; set; }
    public string schema { get; set; }
}


//List Data
public class MergeFields
{
    public string FNAME { get; set; }
    public string LNAME { get; set; }
}

public class Stats
{
    public double avg_open_rate { get; set; }
    public int avg_click_rate { get; set; }
}

public class Location
{
    public double latitude { get; set; }
    public double longitude { get; set; }
    public int gmtoff { get; set; }
    public int dstoff { get; set; }
    public string country_code { get; set; }
    public string timezone { get; set; }
}


public class Member
{
    public string id { get; set; }
    public string email_address { get; set; }
    public string unique_email_id { get; set; }
    public string email_type { get; set; }
    public string status { get; set; }
    public MergeFields merge_fields { get; set; }
    public Stats stats { get; set; }
    public string ip_signup { get; set; }
    public string timestamp_signup { get; set; }
    public string ip_opt { get; set; }
    public string timestamp_opt { get; set; }
    public int member_rating { get; set; }
    public string last_changed { get; set; }
    public string language { get; set; }
    public bool vip { get; set; }
    public string email_client { get; set; }
    public Location location { get; set; }
    public string list_id { get; set; }
    public List<Link> _links { get; set; }
}

Program.cs:

 class Program
{
    static void Main(string[] args)
    {       
        MailChimpApiClient.APIGet("Custom URL Here");   
    }
}

So when I run this and look in the Autos window of VS2013 I see this:

members: Count = 4 System.Collections.Generic.List
[0] {MailChimpAPI.Member} MailChimpAPI.Member
_links: Count = 8 System.Collections.Generic.List
email_address: email address here string
email_client: string
email_type: html string
id: (ID Here) string
ip_opt: (IP Here) string
ip_signup: string
language: string
last_changed: 9/16/15 19:31 string
list_id: (List ID Here) string
location: {MailChimpAPI.Location} MailChimpAPI.Location
member_rating: 3 int
merge_fields {MailChimpAPI.MergeFields} MailChimpAPI.MergeFields
stats: {MailChimpAPI.Stats} MailChimpAPI.Stats
status: unsubscribed string
timestamp_opt: 9/16/15 19:26 string
timestamp_signup: string
unique_email_id: (Unique Email ID Here) string
vip: FALSE bool
[1] {MailChimpAPI.Member}: MailChimpAPI.Member
[2] {MailChimpAPI.Member}: MailChimpAPI.Member
[3] {MailChimpAPI.Member}: MailChimpAPI.Member
Raw View

So for each member (0-3), I need to pull each fields value (Field names in bold) into a variable and save it to a database. I just don't know how to parse through each member.

I know this is a long question for a simple C# problem, but if you can remember way back to the beginning of the post I stated "I am new to C#"

Thanks in advance.

ekim110575
  • 37
  • 4
  • Can't you just use a `ForEach` loop to iterate through `members` and save them one by one in the database? Also, please use [C# Coding Conventions](https://msdn.microsoft.com/en-us/library/ff926074.aspx) for naming your members in the future :) – Arian Motamedi Sep 25 '15 at 18:40
  • Your question is probably not being seen by the correct people. You have tagged it "json" and "mailchimp", however you have already *succesfully* deserialized your JSON and don't need any help there. Instead you want to ask a question like "how to insert data into a sql data table in c#", but you need to tell us what database you are using and what you have tried so far. See for example http://stackoverflow.com/questions/8389803 or http://stackoverflow.com/questions/12568100 or http://stackoverflow.com/questions/21218716/. Suggest you delete/rewrite your question. – dbc Sep 25 '15 at 20:37

0 Answers0