实例介绍
【实例简介】C# 一键GET主流数字货币实时数据
陈立恒博客-专注金融编程:https://www.chenliheng.com
原文链接:https://www.chenliheng.com/findata/getdcdata.html
【实例截图】
【核心代码】
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; namespace GetFinData { public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<string> infoList = new List<string>();//定义infoList,保存解析后的数字货币信息 /// <summary> /// 用HttpWebRequest和HttpWebResponse类发送和接收HTTP数据。 /// </summary> /// <param name="url"></param> /// <param name="Timeout"></param> /// <returns></returns> public static string GetHttpResponse(string url, int Timeout) { //发送 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET";//GET方式通过在网络地址附加参数来完成数据的提交 request.ContentType = "text/html;charset=UTF-8"; request.UserAgent = null; request.Timeout = Timeout;//设置请求的超时值。 //接收 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } private void button1_Click(object sender, EventArgs e) { this.dataGridView1.Rows.Clear();//清空dataGridView try { string json = GetHttpResponse("http://api.coincap.io/v2/assets", 6000);//调用COINCAP API 2.0获取所有数字货币信息 dataShow(json); } catch (Exception ex) { MessageBox.Show("" ex); } } private void button2_Click(object sender, EventArgs e) { createJson(); writeJson(infoList); MessageBox.Show("文件已保存到根目录!","提示"); } /// <summary> /// 提取Json嵌套数组数据并添加到dataGridView /// </summary> /// <param name="json"></param> private void dataShow(string json) { string[] temArr = { "", "", "", "", "", "", "", "", "", "", "" };//数组集合 JObject jo = (JObject)JsonConvert.DeserializeObject(json);//反序列化 JArray jar = JArray.Parse(jo["data"].ToString());//data是原始Json文件中的数组名 for (var i = 0; i < jar.Count; i ) { JObject j = JObject.Parse(jar[i].ToString()); temArr[0] = j["id"].ToString();//unique identifier for asset temArr[1] = j["rank"].ToString();//rank is in ascending order - this number is directly associated with the marketcap whereas the highest marketcap receives rank 1 temArr[2] = j["symbol"].ToString();//most common symbol used to identify this asset on an exchange temArr[3] = j["name"].ToString();//proper name for asset temArr[4] = j["supply"].ToString();//available supply for trading temArr[5] = j["maxSupply"].ToString();//total quantity of asset issued temArr[6] = j["marketCapUsd"].ToString();//supply x price temArr[7] = j["volumeUsd24Hr"].ToString();//quantity of trading volume represented in USD over the last 24 hours temArr[8] = j["priceUsd"].ToString();// volume-weighted price based on real-time market data, translated to USD temArr[9] = j["changePercent24Hr"].ToString();//the direction and value change in the last 24 hours temArr[10] = j["vwap24Hr"].ToString();//Volume Weighted Average Price in the last 24 hours infoList.AddRange(temArr);//添加解析后的数据到List中 this.dataGridView1.Rows.Add(temArr[0], temArr[1], temArr[2], temArr[3], temArr[4], temArr[5], temArr[6], temArr[7], temArr[8], temArr[9], temArr[10]);//dataGridView绑定列值 } } /// <summary> /// 创建Json文件 /// </summary> private void createJson() { //获取当前程序所在路径,并创建CoincapData.json文件 string fp = System.Windows.Forms.Application.StartupPath "\\CoincapData.json"; if (!File.Exists(fp))//判断是否已有相同文件 { FileStream fs1 = new FileStream(fp, FileMode.Create, FileAccess.ReadWrite); fs1.Close(); } } /// <summary> /// 写入Json文件 /// </summary> /// <param name="ob"></param> private void writeJson(object ob) { //获取当前程序所在路径,并写入CoincapData.json文件 string fp = System.Windows.Forms.Application.StartupPath "\\CoincapData.json"; File.WriteAllText(fp, JsonConvert.SerializeObject(ob));//序列化 } private void button3_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("https://www.chenliheng.com");//陈立恒博客-专注金融编程 } private void button4_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("https://www.chenliheng.com/findata/getdcdata.html");//原文链接 } } }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论