通过反序列化的JSON数据进行解析

时间:2015-09-25 18:05:36

标签: c# sql

我是C#的新手。我已经使用简单的控制台应用程序成功使用了Mailchip API。我正在提取相关数据,并且我已经成功地在返回的数据上使用了JSON反序列化器(Newtonsoft.Json通过NuGet)。我需要找到的是将数据放入变量的最佳方法,然后我可以将其传递给存储过程以保存数据。我知道SQL,我知道如何将变量传递给C#中的存储过程(连接字符串等)。

我不知道的是如何解析反序列化的数据,然后将每个值放入一个变量或者我可以传递给SQL的东西。我知道这是一个模糊的问题,所以这是当前的代码。

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]

公共类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");   
    }
}

所以当我运行这个并查看VS2013的Autos窗口时,我看到了:

成员: Count = 4 System.Collections.Generic.List
[0] {MailChimpAPI.Member} MailChimpAPI.Member
     _links: Count = 8 System.Collections.Generic.List
     email_address: 此处的电子邮件地址 字符串
     email_client:字符串
     email_type: html string
     id: (此处为ID) 字符串
     ip_opt: (IP Here) 字符串
     ip_signup:字符串
    语言:字符串
     last_changed: 9/16/15 19:31字符串
     list_id: (此处列出ID) 字符串
    位置: {MailChimpAPI.Location} MailChimpAPI.Location
     member_rating: 3 int
     merge_fields {MailChimpAPI.MergeFields} MailChimpAPI.MergeFields
    统计信息: {MailChimpAPI.Stats} MailChimpAPI.Stats
    状态:取消订阅字符串
     timestamp_opt: 9/16/15 19:26字符串
     timestamp_signup:字符串
     unique_email_id: (此处为唯一电子邮件ID) 字符串
     vip: FALSE bool
[1] {MailChimpAPI.Member}: MailChimpAPI.Member
[2] {MailChimpAPI.Member}: MailChimpAPI.Member
[3] {MailChimpAPI.Member}: MailChimpAPI.Member
原始视图

因此,对于每个成员(0-3),我需要将每个字段值(字段名称以粗体显示)拉入变量并将其保存到数据库中。我只是不知道如何解析每个成员。

我知道这对于一个简单的C#问题来说是一个很长的问题,但是如果你能记得回到帖子的开头,我说“我是C#的新手”

提前致谢。

0 个答案:

没有答案