c#爬取数据并解析json

打印 上一主题 下一主题

主题 859|帖子 859|积分 2577

安装 Newtonsoft.Json

  1. Install-Package Newtonsoft.Json
复制代码
代码

  1. HttpClient client = new HttpClient();
  2. // 获取网页内容
  3. HttpResponseMessage response = client.GetAsync("https://opentdb.com/api.php?amount=10&category=18&difficulty=easy&type=multiple").Result;
  4. response.EnsureSuccessStatusCode();  // 确保请求成功
  5. string content = response.Content.ReadAsStringAsync().Result;
  6. //Console.WriteLine(content);
  7. Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
  8. List<Result> results = JsonConvert.DeserializeObject<List<Result>>(dic["results"].ToString());
  9. foreach(Result result in results)
  10. {
  11.     result.incorrect_answers.ForEach(item =>Console.WriteLine(item));
  12. }
复制代码
Result类

  1. internal class Result
  2. {
  3.     public string type;
  4.     public string difficulty;
  5.     public string category;
  6.     public string question;
  7.     public string correct_answer;
  8.     public List<string> incorrect_answers;
  9. }
复制代码
winform代码

  1. using Newtonsoft.Json;
  2. namespace WinFormsApp1
  3. {
  4.     public partial class Form1 : Form
  5.     {
  6.         List<Result> results = new List<Result>();
  7.         int index = 0;
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.         private void Form1_Load(object sender, EventArgs e)
  13.         {
  14.             HttpClient client = new HttpClient();
  15.             // 获取网页内容
  16.             HttpResponseMessage response = client.GetAsync("https://opentdb.com/api.php?amount=10&category=18&difficulty=easy&type=multiple").Result;
  17.             response.EnsureSuccessStatusCode();  // 确保请求成功
  18.             string content = response.Content.ReadAsStringAsync().Result;
  19.             //Console.WriteLine(content);
  20.             Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
  21.             results = JsonConvert.DeserializeObject<List<Result>>(dic["results"].ToString());
  22.             this.label1.Text = results[index].question;
  23.             List<string> tmpList = new List<string>();
  24.             tmpList.Add(results[index].correct_answer);
  25.             tmpList.Add(results[index].incorrect_answers[0]);
  26.             tmpList.Add(results[index].incorrect_answers[1]);
  27.             tmpList.Add(results[index].incorrect_answers[2]);
  28.             // 使用 Random 和 Sort 打乱顺序
  29.             Random random = new Random();
  30.             tmpList.Sort((a, b) => random.Next(-1, 2));
  31.             this.radioButton1.Text = tmpList[0];
  32.             this.radioButton2.Text = tmpList[1];
  33.             this.radioButton3.Text = tmpList[2];
  34.             this.radioButton4.Text = tmpList[3];
  35.             index++;
  36.             this.button1.Enabled = (index < results.Count);
  37.             this.radioButton1.ForeColor = Color.Black;
  38.             this.radioButton2.ForeColor = Color.Black;
  39.             this.radioButton3.ForeColor = Color.Black;
  40.             this.radioButton4.ForeColor = Color.Black;
  41.             this.radioButton1.Checked = false;
  42.             this.radioButton2.Checked = false;
  43.             this.radioButton3.Checked = false;
  44.             this.radioButton4.Checked = false;
  45.         }
  46.         private void button1_Click(object sender, EventArgs e)
  47.         {
  48.             this.label1.Text = results[index].question;
  49.             List<string> tmpList = new List<string>();
  50.             tmpList.Add(results[index].correct_answer);
  51.             tmpList.Add(results[index].incorrect_answers[0]);
  52.             tmpList.Add(results[index].incorrect_answers[1]);
  53.             tmpList.Add(results[index].incorrect_answers[2]);
  54.             // 使用 Random 和 Sort 打乱顺序
  55.             Random random = new Random();
  56.             tmpList.Sort((a, b) => random.Next(-1, 2));
  57.             this.radioButton1.Text = tmpList[0];
  58.             this.radioButton2.Text = tmpList[1];
  59.             this.radioButton3.Text = tmpList[2];
  60.             this.radioButton4.Text = tmpList[3];
  61.             index++;
  62.             this.button1.Enabled = (index < results.Count);
  63.             this.radioButton1.ForeColor = Color.Black;
  64.             this.radioButton2.ForeColor = Color.Black;
  65.             this.radioButton3.ForeColor = Color.Black;
  66.             this.radioButton4.ForeColor = Color.Black;
  67.             this.radioButton1.Checked = false;
  68.             this.radioButton2.Checked = false;
  69.             this.radioButton3.Checked = false;
  70.             this.radioButton4.Checked = false;
  71.         }
  72.         private void showCorrectAnswer()
  73.         {
  74.             if (this.radioButton1.Text.Equals(this.results[index - 1].correct_answer))
  75.             {
  76.                 this.radioButton1.ForeColor = Color.Blue;
  77.             }
  78.             if (this.radioButton2.Text.Equals(this.results[index - 1].correct_answer))
  79.             {
  80.                 this.radioButton2.ForeColor = Color.Blue;
  81.             }
  82.             if (this.radioButton3.Text.Equals(this.results[index - 1].correct_answer))
  83.             {
  84.                 this.radioButton3.ForeColor = Color.Blue;
  85.             }
  86.             if (this.radioButton4.Text.Equals(this.results[index - 1].correct_answer))
  87.             {
  88.                 this.radioButton4.ForeColor = Color.Blue;
  89.             }
  90.         }
  91.         private void radioButton1_Click(object sender, EventArgs e)
  92.         {
  93.             if (this.radioButton1.Text.Equals(this.results[index - 1].correct_answer))
  94.             {
  95.                 this.radioButton1.ForeColor = Color.Blue;
  96.             }
  97.             else
  98.             {
  99.                 this.radioButton1.ForeColor = Color.Red;
  100.                 showCorrectAnswer();
  101.             }
  102.         }
  103.         private void radioButton2_Click(object sender, EventArgs e)
  104.         {
  105.             if (this.radioButton2.Text.Equals(this.results[index - 1].correct_answer))
  106.             {
  107.                 this.radioButton2.ForeColor = Color.Blue;
  108.             }
  109.             else
  110.             {
  111.                 this.radioButton2.ForeColor = Color.Red;
  112.                 showCorrectAnswer();
  113.             }
  114.         }
  115.         private void radioButton3_Click(object sender, EventArgs e)
  116.         {
  117.             if (this.radioButton3.Text.Equals(this.results[index - 1].correct_answer))
  118.             {
  119.                 this.radioButton3.ForeColor = Color.Blue;
  120.             }
  121.             else
  122.             {
  123.                 this.radioButton3.ForeColor = Color.Red;
  124.                 showCorrectAnswer();
  125.             }
  126.         }
  127.         private void radioButton4_Click(object sender, EventArgs e)
  128.         {
  129.             if (this.radioButton4.Text.Equals(this.results[index - 1].correct_answer))
  130.             {
  131.                 this.radioButton4.ForeColor = Color.Blue;
  132.             }
  133.             else
  134.             {
  135.                 this.radioButton4.ForeColor = Color.Red;
  136.                 showCorrectAnswer();
  137.             }
  138.         }
  139.       
  140.     }
  141. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

千千梦丶琪

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表