C # interface to parse Json

. 1          ///  <Summary> 
2          /// use dynamic loading Json interface data type committed to the database
 . 3          ///  </ Summary> 
. 4          ///  <param name = "Test"> </ param> 
. 5          ///  < Returns> </ Returns> 
. 6          public  String A2 (TestModel Test)
 . 7          {
 . 8              HttpClient Client = new new HttpClient ();
 . 9              var AAA = client.GetStringAsync ( " https://www.layui.com/test/table/demo1. JSON " );
 10              var BBB = aaa.Result;
 . 11              The 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         }
 
 

 

 

 

Guess you like

Origin www.cnblogs.com/wmm0105/p/11764428.html