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.pswp.cn/diannao/70448.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/70448.shtml
英文地址,請注明出處:http://en.pswp.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里面的【最近打開】&#…