• C# 解析Json接口


     1         /// <summary>
     2         /// 使用动态类型加载Json接口数据 提交到数据库
     3         /// </summary>
     4         /// <param name="test"></param>
     5         /// <returns></returns>
     6         public string A2(TestModel test)
     7         {
     8             HttpClient client = new HttpClient();
     9             var aaa = client.GetStringAsync("https://www.layui.com/test/table/demo1.json");
    10             var bbb = aaa.Result;
    11             Stopwatch stopWatch = new Stopwatch();
    12             stopWatch.Start();
    13             var json = JsonConvert.DeserializeObject<dynamic>(bbb);
    14 
    15             for (int i = 0; i < json.data.Count; i++)
    16             {
    17                 test.username = json.data[i].username;
    18                 test.email = json.data[i].email;
    19                 test.sex = json.data[i].sex;
    20                 test.city = json.data[i].city;
    21                 test.sign = json.data[i].sign;
    22                 test.ip = json.data[i].ip;
    23                 test.logins = json.data[i].logins;
    24                 test.joinTime = json.data[i].joinTime;
    25                 string sql = "insert into sssss values(@username,@email,@sex,@city,@sign,@experience,@ip,@logins,@joinTime)";
    26                 using (SqlConnection con = SqlConnectionFactory.Connection)
    27                 {
    28                     con.Execute(sql, test);
    29                 }
    30                 Console.WriteLine(Convert.ToDateTime(json.data[i].joinTime));
    31             }
    32 
    33             stopWatch.Stop();
    34 
    35             TimeSpan ts = stopWatch.Elapsed;
    36 
    37             string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
    38                 ts.Hours, ts.Minutes, ts.Seconds,
    39                 ts.Milliseconds / 10);
    40             Console.WriteLine("RunTime " + elapsedTime);
    41             return bbb;
    42         }
     
     1         /// <summary>
     2         /// 获取Json
     3         /// </summary>
     4         /// <param name="url">https://www.layui.com/test/table/demo1.json</param>
     5         /// <param name="m"></param>
     6         /// <returns></returns>
     7         [HttpGet]
     8         public string Test(string url, TestModel m)
     9         {
    10             if (url.StartsWith("https"))
    11                 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
    12             HttpClient httpClient = new HttpClient();
    13             httpClient.DefaultRequestHeaders.Accept.Add(
    14               new MediaTypeWithQualityHeaderValue("application/json"));
    15             HttpResponseMessage response = httpClient.GetAsync(url).Result;
    16 
    17             if (response.IsSuccessStatusCode)
    18             {
    19                 string result = response.Content.ReadAsStringAsync().Result;
    20                 var listto = Newtonsoft.Json.JsonConvert.DeserializeObject<TestDataModel>(result);
    21                 foreach (var item in listto.data)
    22                 {
    23                     m.id = item.id;
    24                     m.username = item.username;
    25                     m.email = item.email;
    26                     m.sex = item.sex;
    27                     m.city = item.city;
    28                     m.sign = item.sign;
    29                     m.experience = item.experience;
    30                     m.ip = item.ip;
    31                     m.logins = item.logins;
    32                     m.joinTime = item.joinTime;
    33                     string sql = "insert into sssss values(@username,@email,@sex,@city,@sign,@experience,@ip,@logins,@joinTime)";
    34                     using (SqlConnection con = SqlConnectionFactory.Connection)
    35                     {
    36                         con.Execute(sql, m);
    37                     }
    38                 }
    39 
    40                 return result;
    41 
    42             }
    43             return null;
    44 
    45         }
    
    
    
    
    
  • 相关阅读:
    近两周总结、、
    联局域网服务器
    C++ builder 中的 XMLDocument 类详解(17) 更好地显示 xml 的测试结果 FormatXMLData 及其他
    看了《马粥街残酷史》这部电影有感
    这次出差总结
    C++ builder 中的 XMLDocument 类详解(18) NodeValue 与 NodeName 的读写区别
    oracle 常用语句
    c++ builder 中的 XMLDocument 类详解(14) XML的保存与创建
    C++ builder 中的 XMLDocument 类详解(16) – 前一个节点、后一个节点和父节点
    C++ builder 中的 XMLDocument 类详解(15) – Firstnode 和Lastnode
  • 原文地址:https://www.cnblogs.com/wmm0105/p/11764428.html
Copyright © 2020-2023  润新知