序列化c# postman请求到的接口数据存入数据库

 

在postman请求的数据,存入数据库

 1、写结构去访问,

    class BGetAPIApicountResultModel
    {
        public string success { get; set; }
        public string code { get; set; }
        public string message { get; set; }
        public GetAPIApicountEventData data { get; set; }
    }
    public class GetAPIApicountEventData
    { 
        public double ***执行自然人 { get; set; }
        public double ***黑名单 { get; set; }
        public double ***被执行人 { get; set; }
        public double ***人黑名单 { get; set; }
    }

 2 写数据库结构 model

   public class GetAPIApicountSqlModel
    {
        public string MC { get; set; }   
        public double Z { get; set; }  
        public int XH { get; set; }
        public DateTime SJ { get; set; }
    }

 3 方法体里的代码

            try
            {
                string url = APIApicount;
                string data = ApiHelper.Get(url);
                BGetAPIApicountResultModel rm = JsonConvert.DeserializeObject<BGetAPIApicountResultModel>(data);
                List<GetAPIApicountSqlModel> list = new List<GetAPIApicountSqlModel>();
                if (rm != null && rm.data != null)
                {
                    GetAPIApicountSqlModel sm = new GetAPIApicountSqlModel();

                    sm.Z = rm.data.失信被执行自然人;
                    sm.MC = "**行自然人";
                    sm.XH = 1;
                    sm.SJ = DateTime.Now;
                    list.Add(sm);

                    sm = new GetAPIApicountSqlModel();
                    sm.Z = rm.data.法人黑名单;
                    sm.MC = "**人黑名单";
                    sm.XH = 2;
                    sm.SJ = DateTime.Now;
                    list.Add(sm); 

                    string namse = DataHelper.GetSqlNamesByModel(new GetAPIApicountSqlModel());
                    string values = DataHelper.GetSqlValuesByModel(list, new GetAPIApicountSqlModel());
                    MySqlHelper.ClearDataByName("zdcw_tb_sxfx", connection);
                    MySqlHelper.InsertDataByName("zdcw_tb_sxfx", namse, values, connection);
                    Console.WriteLine(DateTime.Now + "***信黑名单人员统计数据入库成功");
                }
                else
                {
                    Console.WriteLine(DateTime.Now + "失信黑名单人员统计无数据");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error___获取失信黑名单人员统计析" + DateTime.Now + ex.Message);
                WriteLogsHelper.ErrorLogs("获取***黑名单人员统计", ex.Message, "**信黑名单人员统计");
            }
        }

具体的sqlhelper方法我就不在这详细写了。

需要注意的是结构,不同的结构需要写不同的方法。

举个例子:

 

  public  class statEducationResultModel
    {
        public string success { get; set; }
        public string code { get; set; }
        public string message { get; set; }
        public statEducationbData data { get; set; }
        public class statEducationbData
        {
            public List<HighSchool> HighSchool { get; set; }
            public List<Kindergarten> Kindergarten { get; set; }
            public List<PrimarySchool> PrimarySchool { get; set; }
            public List<JuniorMiddleSchool> JuniorMiddleSchool { get; set; }
        }
        public class HighSchool
        {
            public string code { set; get; }
            public string name { set; get; }
            public double num { get; set; } 

        }
        public class Kindergarten
        {
            public string code { set; get; }
            public string name { set; get; }
            public double num { get; set; } 

        }
        public class PrimarySchool
        {
            public string code { set; get; }
            public string name { set; get; }
            public double num { get; set; } 

        }
        public class JuniorMiddleSchool
        {
            public string code { set; get; }
            public string name { set; get; }
            public double num { get; set; } 

        }

 循环读取,代码逻辑

statEducationResultModel rqm = new statEducationResultModel();
                string postData = JsonConvert.SerializeObject(rqm);
                string url = statEducation;
                string data = ApiHelper.Post(url, postData);
                statEducationResultModel rm = JsonConvert.DeserializeObject<statEducationResultModel>(data);
                List<statEducationSqlModel> list = new List<statEducationSqlModel>();
                if (rm != null && rm.data != null && rm.data.HighSchool.Count > 0)
                {
                    

                    Dictionary<string, double> dic = new Dictionary<string, double>();
                    foreach (var item in rm.data.HighSchool)
                    {
                        if (dic.ContainsKey(item.name))
                        {
                            dic[item.name]+=item.num;
                        }
                        else
                        {
                            dic[item.name] = item.num;
                        }
                    } 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值