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.mzph.cn/diannao/70448.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机毕业设计Python农产品推荐系统 农产品爬虫 农产品可视化 农产品大数据(源码+LW文档+PPT+讲解)

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 作者简介&#xff1a;Java领…

【分布式理论13】分布式存储:数据存储难题与解决之道

文章目录 一、数据存储面临的问题二、RAID磁盘阵列的解决方案1. RAID概述2. RAID使用的技术3. RAID的代表性等级 三、分布式存储的新思路1. 分布式存储背景与特点2. 分布式存储的组成要素 一、数据存储面临的问题 在单机系统时代&#xff0c;当数据量不断增加、硬盘空间不够时…

高德地图android sdk(备忘)

依赖 // 权限请求框架&#xff1a;https://github.com/getActivity/XXPermissions implementation com.github.getActivity:XXPermissions:20.0 // https://mvnrepository.com/artifact/com.amap.api/navi-3dmap-location-search implementation com.amap.api:navi-3dmap-loca…

DeepSeek、微信、硅基流动、纳米搜索、秘塔搜索……十种不同方法实现DeepSeek使用自由

为了让大家实现 DeepSeek 使用自由&#xff0c;今天分享 10 个畅用 DeepSeek 的平台。 一、官方满血版&#xff1a;DeepSeek官网与APP 首推&#xff0c;肯定是 DeepSeek 的官网和 APP&#xff0c;可以使用满血版 R1 和 V3 模型&#xff0c;以及联网功能。 网址&#xff1a; htt…

自动化之ansible(二)

一、ansible中playbook&#xff08;剧本&#xff09; 官方文档&#xff1a; Ansible playbooks — Ansible Community Documentation 1、playbook的基本结构 一个基本的playbook由以下几个主要部分组成 hosts: 定义要执行任务的主机组或主机。 become: 是否需要使用超级用户…

python 神经网络教程,神经网络模型代码python,小白入门基础教程

文章目录 前言1. 神经网络基础概念1.1 神经元1.2 激活函数1.3 神经网络结构1.4 安装 Python1.5 选择开发环境2. 使用numpy构建简单神经网络 3. 使用PyTorch构建复杂神经网络 前言 本教程旨在为广大初学者和有一定基础的开发者提供一个系统、全面且深入的 Python 神经网络学习指…

SOME/IP--协议英文原文讲解10

前言 SOME/IP协议越来越多的用于汽车电子行业中&#xff0c;关于协议详细完全的中文资料却没有&#xff0c;所以我将结合工作经验并对照英文原版协议做一系列的文章。基本分三大块&#xff1a; 1. SOME/IP协议讲解 2. SOME/IP-SD协议讲解 3. python/C举例调试讲解 4.2.2 Req…

Spring框架基本使用(Maven详解)

前言&#xff1a; 当我们创建项目的时候&#xff0c;第一步少不了搭建环境的相关准备工作。 那么如果想让我们的项目做起来方便快捷&#xff0c;应该引入更多的管理工具&#xff0c;帮我们管理。 Maven的出现帮我们大大解决了管理的难题&#xff01;&#xff01; Maven&#xf…

用大内存主机下载Visual Studio

用一台内存达到128G的主机下载Visual Studio 2022&#xff0c;用的是公司网络。下载速度让我吃了一惊&#xff0c;没人用网络了&#xff1f;还是网站提速了&#xff1f;以前最大只能达到5MB/秒。记录这段经历&#xff0c;是用来分析公司网络用的......

Ubuntu20.04.2安装Vmware tools

软件版本&#xff1a;Vmware Workstation Pro 17.6.2 操作系统镜像文件&#xff1a;ubuntu-20.04.2-desktop-amd64 方式1&#xff1a;用iso镜像安装 没用这种方法&#xff0c;太麻烦 方式2&#xff1a;用apt安装Open VM Tools 如果你使用的是较新的Ubuntu版本&#xff08;如…

Mac系统下使用Docker快速部署MaxKB:打造本地知识库问答系统

随着大语言模型的广泛应用&#xff0c;知识库问答系统逐渐成为提升工作效率和个人学习的有力工具。MaxKB是一款基于LLM&#xff08;Large Language Model&#xff09;大语言模型的知识库问答系统&#xff0c;支持多模型对接、文档上传和自动爬取等功能。本文将详细介绍如何在Ma…

2526考研资料分享 百度网盘

通过网盘分享的文件&#xff1a;01、2026【考研数学】 链接: https://pan.baidu.com/s/1N-TlXcCKMcX1U-KBr0Oejg?pwducbe 提取码: ucbe 提取码:98wg--来自百度网盘超级会员v3的分享 通过网盘分享的文件&#xff1a;01、2026【考研政治】 链接: https://pan.baidu.com/s/1N-T…

【信息系统项目管理师-案例真题】2013下半年案例分析答案和详解

更多内容请见: 备考信息系统项目管理师-专栏介绍和目录 文章目录 试题一【问题‍ 1】(12 分)【问题‍ 2】(10 分)【问题 3】( 3 分 )试题二【问题‍ 1】(3‍ 分)【问题‍ 2】(10‍ 分)【问题‍ 3】(3‍ 分)【问题‍ 4】(9‍ 分)试题三【问题‍ 1】(12‍ 分)‍【问题‍ 2】(8‍…

idea连接gitee(使用idea远程兼容gitee)

文章目录 先登录你的gitee拿到你的邮箱找到idea的设置选择密码方式登录填写你的邮箱和密码登录成功 先登录你的gitee拿到你的邮箱 具体位置在gitee–>设置–>邮箱管理 找到idea的设置 选择密码方式登录 填写你的邮箱和密码 登录成功

UNIAPP开发之利用阿里RTC服务实现音视频通话后端THINKPHP5

下面是一个使用ThinkPHP 5实现后端逻辑的示例。我们将创建一个简单的ThinkPHP 5项目来处理生成推流和播流地址的请求。 后端部分&#xff08;ThinkPHP 5&#xff09; 1. 初始化ThinkPHP 5项目 首先&#xff0c;确保你已经安装了Composer。然后使用Composer创建一个新的Think…

嵌入式开发岗位认识

目录 1.核心定义2.岗位方向3.行业方向4.技术方向5.工作职责6.核心技能7.等级标准8.优势与劣势9.市场薪资10. 发展路径11. 市场趋势12. 技术趋势 1.核心定义 嵌入式系统&#xff1a; 以应用为中心&#xff0c;以计算机技术为基础&#xff0c;软硬件可裁剪的专用计算机系统 特点…

图解循环神经网络(RNN)

目录 1.循环神经网络介绍 2.网络结构 3.结构分类 4.模型工作原理 5.模型工作示例 6.总结 1.循环神经网络介绍 RNN&#xff08;Recurrent Neural Network&#xff0c;循环神经网络&#xff09;是一种专门用于处理序列数据的神经网络结构。与传统的神经网络不同&#xff0c…

MySQL数据库入门到大蛇尚硅谷宋红康老师笔记 高级篇 part 2

第02章_MySQL的数据目录 1. MySQL8的主要目录结构 1.1 数据库文件的存放路径 MySQL数据库文件的存放路径&#xff1a;/var/lib/mysql/ MySQL服务器程序在启动时会到文件系统的某个目录下加载一些文件&#xff0c;之后在运行过程中产生的数据也都会存储到这个目录下的某些文件…

http 响应码影响 video 标签播放视频

背景 使用后端给的文件下载接口地址实现视频播放&#xff0c;但是 video 标签一直无法播放视频如下图&#xff0c;把接口地址放到浏览器请求能直接下载。但就是不能播放 原因 http 响应码不正确&#xff0c;返回201是无法播放视频200可以如下图 状态码的影响&#xff1a; 20…

OneNote手机/平板“更多笔记本”中有许多已经删掉或改名的,如何删除

问题描述&#xff1a; OneNote 在手机或平板上添加“更多笔记本”中&#xff0c;有许多已经删掉或改名的笔记本&#xff01;如何删除&#xff1f; OR&#xff1a;如何彻底删除OneNote中的笔记本&#xff1f; 处理做法&#xff1a; 这个列表对应365里面的【最近打开】&#…