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

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();
            }
        }

      
    }
}

在这里插入图片描述

相关文章:

  • PH热榜 | 2025-02-20
  • 美国第1代哈希散列算法SHA-1
  • 【自动化脚本工具】AutoHotkey (Windows)
  • 力扣的第34题 在排序数组中查找元素的第一个和最后一个位置
  • 深入理解 MySQL 8 C++ 源码:SELECT MOD(MONTH(NOW()), 2) 的函数执行过程
  • Cross-correlation 加速算法公式推导
  • 算法从0到100之【专题一】- 双指针第一练(数组划分、数组分块)
  • mysql云上安装慢问题解决
  • nasm - BasicWindow_64
  • 关于重启Pod后,CNI网络插件问题报错
  • AI 内容检测工具全解析,助力内容创作无忧
  • 蓝桥杯备考策略
  • Linux常用操作
  • java每日精进 2.20 MQ相关复健
  • 【量化策略】均值回归策略
  • 蓝桥杯 2.基础算法
  • gen_gauss_filter用于检测带方向的线条
  • 运维脚本——6.资源优化
  • 斐波那契数列模型:在动态规划的丝绸之路上追寻斐波那契的足迹(下)
  • 系统思考—价格策略
  • 北京公园使用指南
  • “中国游”带火“中国购”,“即买即退”让外国游客购物更丝滑
  • 下任美联储主席热门人选沃什:美联储犯下“系统性错误”,未能控制一代人以来最严重的通胀
  • 伊朗最大港口爆炸:26公里外都能听到,超七百人受伤,原因指向化学品储存
  • 大学2025丨专访北邮校长徐坤:工科教育要真正回归工程本质
  • 伊朗阿巴斯港港口爆炸已致47人受伤