当前位置: 首页 > article >正文

c#爬取数据并解析json

安装 Newtonsoft.Json

Install-Package Newtonsoft.Json

代码

HttpClient client = new HttpClient();

// 获取网页内容

HttpResponseMessage response = client.GetAsync("https://opentdb.com/api.php?amount=10&category=18&difficulty=easy&type=multiple").Result;
response.EnsureSuccessStatusCode();  // 确保请求成功
string content = response.Content.ReadAsStringAsync().Result;
//Console.WriteLine(content);
Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
List<Result> results = JsonConvert.DeserializeObject<List<Result>>(dic["results"].ToString());
foreach(Result result in results)
{
    result.incorrect_answers.ForEach(item =>Console.WriteLine(item));
}

Result类

internal class Result
{
    public string type;
    public string difficulty;
    public string category;
    public string question;
    public string correct_answer;
    public List<string> incorrect_answers;
}

winform代码

using Newtonsoft.Json;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        List<Result> results = new List<Result>();
        int index = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();

            // 获取网页内容

            HttpResponseMessage response = client.GetAsync("https://opentdb.com/api.php?amount=10&category=18&difficulty=easy&type=multiple").Result;
            response.EnsureSuccessStatusCode();  // 确保请求成功
            string content = response.Content.ReadAsStringAsync().Result;
            //Console.WriteLine(content);
            Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
            results = JsonConvert.DeserializeObject<List<Result>>(dic["results"].ToString());


            this.label1.Text = results[index].question;
            List<string> tmpList = new List<string>();
            tmpList.Add(results[index].correct_answer);
            tmpList.Add(results[index].incorrect_answers[0]);
            tmpList.Add(results[index].incorrect_answers[1]);
            tmpList.Add(results[index].incorrect_answers[2]);
            // 使用 Random 和 Sort 打乱顺序
            Random random = new Random();
            tmpList.Sort((a, b) => random.Next(-1, 2));
            this.radioButton1.Text = tmpList[0];
            this.radioButton2.Text = tmpList[1];
            this.radioButton3.Text = tmpList[2];
            this.radioButton4.Text = tmpList[3];
            index++;
            this.button1.Enabled = (index < results.Count);
            this.radioButton1.ForeColor = Color.Black;
            this.radioButton2.ForeColor = Color.Black;
            this.radioButton3.ForeColor = Color.Black;
            this.radioButton4.ForeColor = Color.Black;

            this.radioButton1.Checked = false;
            this.radioButton2.Checked = false;
            this.radioButton3.Checked = false;
            this.radioButton4.Checked = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.label1.Text = results[index].question;
            List<string> tmpList = new List<string>();
            tmpList.Add(results[index].correct_answer);
            tmpList.Add(results[index].incorrect_answers[0]);
            tmpList.Add(results[index].incorrect_answers[1]);
            tmpList.Add(results[index].incorrect_answers[2]);
            // 使用 Random 和 Sort 打乱顺序
            Random random = new Random();
            tmpList.Sort((a, b) => random.Next(-1, 2));
            this.radioButton1.Text = tmpList[0];
            this.radioButton2.Text = tmpList[1];
            this.radioButton3.Text = tmpList[2];
            this.radioButton4.Text = tmpList[3];
            index++;
            this.button1.Enabled = (index < results.Count);
            this.radioButton1.ForeColor = Color.Black;
            this.radioButton2.ForeColor = Color.Black;
            this.radioButton3.ForeColor = Color.Black;
            this.radioButton4.ForeColor = Color.Black;

            this.radioButton1.Checked = false;
            this.radioButton2.Checked = false;
            this.radioButton3.Checked = false;
            this.radioButton4.Checked = false;
        }

        private void showCorrectAnswer()
        {
            if (this.radioButton1.Text.Equals(this.results[index - 1].correct_answer))
            {
                this.radioButton1.ForeColor = Color.Blue;
            }
            if (this.radioButton2.Text.Equals(this.results[index - 1].correct_answer))
            {
                this.radioButton2.ForeColor = Color.Blue;
            }
            if (this.radioButton3.Text.Equals(this.results[index - 1].correct_answer))
            {
                this.radioButton3.ForeColor = Color.Blue;
            }
            if (this.radioButton4.Text.Equals(this.results[index - 1].correct_answer))
            {
                this.radioButton4.ForeColor = Color.Blue;
            }
        }
        private void radioButton1_Click(object sender, EventArgs e)
        {
            if (this.radioButton1.Text.Equals(this.results[index - 1].correct_answer))
            {
                this.radioButton1.ForeColor = Color.Blue;
            }
            else
            {
                this.radioButton1.ForeColor = Color.Red;
                showCorrectAnswer();
            }
        }

        private void radioButton2_Click(object sender, EventArgs e)
        {
            if (this.radioButton2.Text.Equals(this.results[index - 1].correct_answer))
            {
                this.radioButton2.ForeColor = Color.Blue;
            }
            else
            {
                this.radioButton2.ForeColor = Color.Red;
                showCorrectAnswer();
            }
        }

        private void radioButton3_Click(object sender, EventArgs e)
        {
            if (this.radioButton3.Text.Equals(this.results[index - 1].correct_answer))
            {
                this.radioButton3.ForeColor = Color.Blue;
            }
            else
            {
                this.radioButton3.ForeColor = Color.Red;
                showCorrectAnswer();
            }
        }

        private void radioButton4_Click(object sender, EventArgs e)
        {
            if (this.radioButton4.Text.Equals(this.results[index - 1].correct_answer))
            {
                this.radioButton4.ForeColor = Color.Blue;
            }
            else
            {
                this.radioButton4.ForeColor = Color.Red;
                showCorrectAnswer();
            }
        }

      
    }
}

在这里插入图片描述


http://www.kler.cn/a/555141.html

相关文章:

  • 命令注入绕过
  • c# 2025/2/19 周三
  • 《95015网络安全应急响应分析报告(2024)》
  • Spring MVC中ViewControllerRegistry的使用与原理
  • springboot2.0整合logback日志(保姆级详细教程)
  • 鸿蒙next中如何使用hdc命令安装客户端包
  • 简单易懂,解析Go语言中的Channel管道
  • 【Java从入门到起飞】流程控制语句
  • 《跟李沐学 AI》AlexNet论文逐段精读学习心得 | PyTorch 深度学习实战
  • 【干货分享】Autosar CanIf 模块的应用干货笔记1
  • Python使用过程中哪些字符是非法的?
  • 【Linux】多线程 -> 线程同步与基于BlockingQueue的生产者消费者模型
  • 分割回文串力扣--131
  • virtualbox怎么把主机剪切板里的内容复制进来
  • 人工智能之视频分割模型sam2源码解读
  • DeepSeek全栈技术体系解密:从算法源码到企业级智能体开发实战
  • [Windows] WPS 2024冬季更新版(版本号19770)
  • MYSQL总结(3)
  • 测试WSS服务器
  • 在UBUNTU下搭建Deepseek