豆瓣API查询和显示图书的信息

第一步:GetDataSource.cs类,用来通过HTTP协议从网络获取json文件,并通过文件流操作将json转化为string类型返回

第二步:通过c#语言在visual studio上画图形界面

第三步:引用”using Newtonsoft.Json.Linq;”,使用JObject来解析返回的string类型的json文件,获取符合关键字”q”的所有图书
遍历文件获取图书信息,将内容添加带List集合上,并绑定到datagridview.显示在窗体上;

第四步:通过传参数(图书的API),获取每本图书的信息,并显示在WinForm窗体上。

第五步:修改程序,使用多线程和委托技术,防止在获取大量数据时候界面卡死情况。


Books.cs

 public class Books
    {
        /// <summary>
        ///     书名
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        ///     作者
        /// </summary>
        public string Author { get; set; }

        /// <summary>
        ///     API地址
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        ///     页数
        /// </summary>
        public string Pages { get; set; }

        /// <summary>
        ///     价格
        /// </summary>
        public string Price { get; set; }

        /// <summary>
        ///     序号
        /// </summary>
        public int Index { get; set; }
    }

GetDataSource.cs

  public class GetDataSource
    {
        /// <summary>
        ///     通过http协议从网络上获取json文件内容
        /// </summary>
        /// <param name="url">API地址</param>
        /// <returns>将json内容以string类型返回</returns>
        public string GetJsonFile(string url)
        {
            HttpWebRequest myRequest = null;
            HttpWebResponse myHttpResponse = null;
            string getUrl = url.Replace(@"\", "").Trim();
            myRequest = (HttpWebRequest) WebRequest.Create(getUrl);
            myHttpResponse = (HttpWebResponse) myRequest.GetResponse();
            var reader = new StreamReader(myHttpResponse.GetResponseStream(), Encoding.UTF8);
            string jsonFile = reader.ReadToEnd();
            reader.Close();
            myHttpResponse.Close();
            return jsonFile;
        }

        /// <summary>
        ///     通过标签获得图书信息
        /// </summary>
        /// <param name="tags">标签</param>
        /// <returns>将json内容以string类型返回</returns>
        public string GetTagsInfo(string tags)
        {
            string temp = "https://api.douban.com/v2/book/search?q=" + tags + "&start=0&count=100";
            var myRequest = (HttpWebRequest) WebRequest.Create(temp);
            var myHttpResponse = (HttpWebResponse) myRequest.GetResponse();
            var reader = new StreamReader(myHttpResponse.GetResponseStream(), Encoding.UTF8);
            string tagsFile = reader.ReadToEnd();
            reader.Close();
            myHttpResponse.Close();
            return tagsFile;
        }
    }

ReadAPIForm.Designer.cs

   partial class ReadAPIForm
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ReadAPIForm));
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.btnSearch = new System.Windows.Forms.Button();
            this.tbSearch = new System.Windows.Forms.TextBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.dgvBookInfo = new System.Windows.Forms.DataGridView();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column4 = new System.Windows.Forms.DataGridViewLinkColumn();
            this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column6 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.panel1 = new System.Windows.Forms.Panel();
            this.richTbSummery = new System.Windows.Forms.RichTextBox();
            this.btnCancle = new System.Windows.Forms.Button();
            this.labelBookPrice = new System.Windows.Forms.Label();
            this.labelBookPages = new System.Windows.Forms.Label();
            this.labelBookBinding = new System.Windows.Forms.Label();
            this.labelBookPublishDate = new System.Windows.Forms.Label();
            this.labelBookPublisher = new System.Windows.Forms.Label();
            this.labelBookTranslator = new System.Windows.Forms.Label();
            this.labelBookAuthor = new System.Windows.Forms.Label();
            this.labelBookID = new System.Windows.Forms.Label();
            this.labelBookName = new System.Windows.Forms.Label();
            this.labelSummery = new System.Windows.Forms.Label();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            this.labelPrice = new System.Windows.Forms.Label();
            this.labelPages = new System.Windows.Forms.Label();
            this.labelBinding = new System.Windows.Forms.Label();
            this.labelPubTime = new System.Windows.Forms.Label();
            this.labelPublisher = new System.Windows.Forms.Label();
            this.labelTrans = new System.Windows.Forms.Label();
            this.labelAuthor = new System.Windows.Forms.Label();
            this.labelID = new System.Windows.Forms.Label();
            this.labelTitle = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dgvBookInfo)).BeginInit();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.btnSearch);
            this.groupBox1.Controls.Add(this.tbSearch);
            this.groupBox1.Controls.Add(this.pictureBox1);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.groupBox1.Size = new System.Drawing.Size(747, 98);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            // 
            // btnSearch
            // 
            this.btnSearch.Location = new System.Drawing.Point(452, 48);
            this.btnSearch.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.btnSearch.Name = "btnSearch";
            this.btnSearch.Size = new System.Drawing.Size(100, 29);
            this.btnSearch.TabIndex = 2;
            this.btnSearch.Text = "搜索";
            this.btnSearch.UseVisualStyleBackColor = true;
            this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
            // 
            // tbSearch
            // 
            this.tbSearch.Location = new System.Drawing.Point(200, 48);
            this.tbSearch.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.tbSearch.Name = "tbSearch";
            this.tbSearch.Size = new System.Drawing.Size(241, 25);
            this.tbSearch.TabIndex = 1;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(69, 25);
            this.pictureBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(67, 62);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // dgvBookInfo
            // 
            this.dgvBookInfo.AllowUserToResizeRows = false;
            this.dgvBookInfo.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvBookInfo.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1,
            this.Column2,
            this.Column3,
            this.Column4,
            this.Column5,
            this.Column6});
            this.dgvBookInfo.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dgvBookInfo.Location = new System.Drawing.Point(0, 98);
            this.dgvBookInfo.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.dgvBookInfo.Name = "dgvBookInfo";
            this.dgvBookInfo.RowHeadersVisible = false;
            this.dgvBookInfo.RowTemplate.Height = 23;
            this.dgvBookInfo.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dgvBookInfo.Size = new System.Drawing.Size(747, 521);
            this.dgvBookInfo.TabIndex = 1;
            this.dgvBookInfo.Visible = false;
            this.dgvBookInfo.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvBookInfo_CellContentClick);
            // 
            // Column1
            // 
            this.Column1.DataPropertyName = "Index";
            this.Column1.FillWeight = 76.14214F;
            this.Column1.HeaderText = "序号";
            this.Column1.Name = "Column1";
            this.Column1.Width = 25;
            // 
            // Column2
            // 
            this.Column2.DataPropertyName = "Title";
            this.Column2.FillWeight = 104.7716F;
            this.Column2.HeaderText = "书名";
            this.Column2.Name = "Column2";
            // 
            // Column3
            // 
            this.Column3.DataPropertyName = "Author";
            this.Column3.FillWeight = 104.7716F;
            this.Column3.HeaderText = "作者";
            this.Column3.Name = "Column3";
            // 
            // Column4
            // 
            this.Column4.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.Column4.DataPropertyName = "Url";
            this.Column4.FillWeight = 104.7716F;
            this.Column4.HeaderText = "图书API";
            this.Column4.Name = "Column4";
            this.Column4.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.Column4.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            // 
            // Column5
            // 
            this.Column5.DataPropertyName = "Pages";
            this.Column5.FillWeight = 104.7716F;
            this.Column5.HeaderText = "页数";
            this.Column5.Name = "Column5";
            this.Column5.Width = 54;
            // 
            // Column6
            // 
            this.Column6.DataPropertyName = "Price";
            this.Column6.FillWeight = 104.7716F;
            this.Column6.HeaderText = "价格";
            this.Column6.Name = "Column6";
            this.Column6.Width = 54;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.richTbSummery);
            this.panel1.Controls.Add(this.btnCancle);
            this.panel1.Controls.Add(this.labelBookPrice);
            this.panel1.Controls.Add(this.labelBookPages);
            this.panel1.Controls.Add(this.labelBookBinding);
            this.panel1.Controls.Add(this.labelBookPublishDate);
            this.panel1.Controls.Add(this.labelBookPublisher);
            this.panel1.Controls.Add(this.labelBookTranslator);
            this.panel1.Controls.Add(this.labelBookAuthor);
            this.panel1.Controls.Add(this.labelBookID);
            this.panel1.Controls.Add(this.labelBookName);
            this.panel1.Controls.Add(this.labelSummery);
            this.panel1.Controls.Add(this.pictureBox2);
            this.panel1.Controls.Add(this.labelPrice);
            this.panel1.Controls.Add(this.labelPages);
            this.panel1.Controls.Add(this.labelBinding);
            this.panel1.Controls.Add(this.labelPubTime);
            this.panel1.Controls.Add(this.labelPublisher);
            this.panel1.Controls.Add(this.labelTrans);
            this.panel1.Controls.Add(this.labelAuthor);
            this.panel1.Controls.Add(this.labelID);
            this.panel1.Controls.Add(this.labelTitle);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 98);
            this.panel1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(747, 521);
            this.panel1.TabIndex = 2;
            this.panel1.Visible = false;
            // 
            // richTbSummery
            // 
            this.richTbSummery.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.richTbSummery.Location = new System.Drawing.Point(69, 365);
            this.richTbSummery.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.richTbSummery.Name = "richTbSummery";
            this.richTbSummery.ReadOnly = true;
            this.richTbSummery.Size = new System.Drawing.Size(660, 140);
            this.richTbSummery.TabIndex = 22;
            this.richTbSummery.Text = "";
            // 
            // btnCancle
            // 
            this.btnCancle.Location = new System.Drawing.Point(631, 8);
            this.btnCancle.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.btnCancle.Name = "btnCancle";
            this.btnCancle.Size = new System.Drawing.Size(100, 29);
            this.btnCancle.TabIndex = 21;
            this.btnCancle.Text = "返回";
            this.btnCancle.UseVisualStyleBackColor = true;
            this.btnCancle.Click += new System.EventHandler(this.btnCancle_Click);
            // 
            // labelBookPrice
            // 
            this.labelBookPrice.AutoSize = true;
            this.labelBookPrice.Location = new System.Drawing.Point(401, 331);
            this.labelBookPrice.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelBookPrice.Name = "labelBookPrice";
            this.labelBookPrice.Size = new System.Drawing.Size(55, 15);
            this.labelBookPrice.TabIndex = 20;
            this.labelBookPrice.Text = "label9";
            // 
            // labelBookPages
            // 
            this.labelBookPages.AutoSize = true;
            this.labelBookPages.Location = new System.Drawing.Point(401, 296);
            this.labelBookPages.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelBookPages.Name = "labelBookPages";
            this.labelBookPages.Size = new System.Drawing.Size(55, 15);
            this.labelBookPages.TabIndex = 19;
            this.labelBookPages.Text = "label8";
            // 
            // labelBookBinding
            // 
            this.labelBookBinding.AutoSize = true;
            this.labelBookBinding.Location = new System.Drawing.Point(401, 268);
            this.labelBookBinding.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelBookBinding.Name = "labelBookBinding";
            this.labelBookBinding.Size = new System.Drawing.Size(55, 15);
            this.labelBookBinding.TabIndex = 18;
            this.labelBookBinding.Text = "label7";
            // 
            // labelBookPublishDate
            // 
            this.labelBookPublishDate.AutoSize = true;
            this.labelBookPublishDate.Location = new System.Drawing.Point(401, 228);
            this.labelBookPublishDate.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelBookPublishDate.Name = "labelBookPublishDate";
            this.labelBookPublishDate.Size = new System.Drawing.Size(55, 15);
            this.labelBookPublishDate.TabIndex = 17;
            this.labelBookPublishDate.Text = "label6";
            // 
            // labelBookPublisher
            // 
            this.labelBookPublisher.AutoSize = true;
            this.labelBookPublisher.Location = new System.Drawing.Point(401, 191);
            this.labelBookPublisher.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelBookPublisher.Name = "labelBookPublisher";
            this.labelBookPublisher.Size = new System.Drawing.Size(55, 15);
            this.labelBookPublisher.TabIndex = 16;
            this.labelBookPublisher.Text = "label5";
            // 
            // labelBookTranslator
            // 
            this.labelBookTranslator.AutoSize = true;
            this.labelBookTranslator.Location = new System.Drawing.Point(401, 152);
            this.labelBookTranslator.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelBookTranslator.Name = "labelBookTranslator";
            this.labelBookTranslator.Size = new System.Drawing.Size(55, 15);
            this.labelBookTranslator.TabIndex = 15;
            this.labelBookTranslator.Text = "label4";
            // 
            // labelBookAuthor
            // 
            this.labelBookAuthor.AutoSize = true;
            this.labelBookAuthor.Location = new System.Drawing.Point(401, 121);
            this.labelBookAuthor.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelBookAuthor.Name = "labelBookAuthor";
            this.labelBookAuthor.Size = new System.Drawing.Size(55, 15);
            this.labelBookAuthor.TabIndex = 14;
            this.labelBookAuthor.Text = "label3";
            // 
            // labelBookID
            // 
            this.labelBookID.AutoSize = true;
            this.labelBookID.Location = new System.Drawing.Point(401, 85);
            this.labelBookID.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelBookID.Name = "labelBookID";
            this.labelBookID.Size = new System.Drawing.Size(55, 15);
            this.labelBookID.TabIndex = 13;
            this.labelBookID.Text = "label2";
            // 
            // labelBookName
            // 
            this.labelBookName.AutoSize = true;
            this.labelBookName.Location = new System.Drawing.Point(401, 50);
            this.labelBookName.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelBookName.Name = "labelBookName";
            this.labelBookName.Size = new System.Drawing.Size(55, 15);
            this.labelBookName.TabIndex = 12;
            this.labelBookName.Text = "label1";
            // 
            // labelSummery
            // 
            this.labelSummery.AutoSize = true;
            this.labelSummery.Location = new System.Drawing.Point(20, 365);
            this.labelSummery.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelSummery.Name = "labelSummery";
            this.labelSummery.Size = new System.Drawing.Size(52, 15);
            this.labelSummery.TabIndex = 10;
            this.labelSummery.Text = "概要:";
            // 
            // pictureBox2
            // 
            this.pictureBox2.Location = new System.Drawing.Point(16, 24);
            this.pictureBox2.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(289, 322);
            this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox2.TabIndex = 9;
            this.pictureBox2.TabStop = false;
            // 
            // labelPrice
            // 
            this.labelPrice.AutoSize = true;
            this.labelPrice.Location = new System.Drawing.Point(339, 331);
            this.labelPrice.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelPrice.Name = "labelPrice";
            this.labelPrice.Size = new System.Drawing.Size(52, 15);
            this.labelPrice.TabIndex = 8;
            this.labelPrice.Text = "价格:";
            // 
            // labelPages
            // 
            this.labelPages.AutoSize = true;
            this.labelPages.Location = new System.Drawing.Point(339, 296);
            this.labelPages.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelPages.Name = "labelPages";
            this.labelPages.Size = new System.Drawing.Size(52, 15);
            this.labelPages.TabIndex = 7;
            this.labelPages.Text = "页数:";
            // 
            // labelBinding
            // 
            this.labelBinding.AutoSize = true;
            this.labelBinding.Location = new System.Drawing.Point(339, 268);
            this.labelBinding.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelBinding.Name = "labelBinding";
            this.labelBinding.Size = new System.Drawing.Size(52, 15);
            this.labelBinding.TabIndex = 6;
            this.labelBinding.Text = "装帧:";
            // 
            // labelPubTime
            // 
            this.labelPubTime.AutoSize = true;
            this.labelPubTime.Location = new System.Drawing.Point(307, 228);
            this.labelPubTime.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelPubTime.Name = "labelPubTime";
            this.labelPubTime.Size = new System.Drawing.Size(82, 15);
            this.labelPubTime.TabIndex = 5;
            this.labelPubTime.Text = "出版时间:";
            // 
            // labelPublisher
            // 
            this.labelPublisher.AutoSize = true;
            this.labelPublisher.Location = new System.Drawing.Point(323, 191);
            this.labelPublisher.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelPublisher.Name = "labelPublisher";
            this.labelPublisher.Size = new System.Drawing.Size(67, 15);
            this.labelPublisher.TabIndex = 4;
            this.labelPublisher.Text = "出版社:";
            // 
            // labelTrans
            // 
            this.labelTrans.AutoSize = true;
            this.labelTrans.Location = new System.Drawing.Point(339, 152);
            this.labelTrans.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelTrans.Name = "labelTrans";
            this.labelTrans.Size = new System.Drawing.Size(52, 15);
            this.labelTrans.TabIndex = 3;
            this.labelTrans.Text = "翻译:";
            // 
            // labelAuthor
            // 
            this.labelAuthor.AutoSize = true;
            this.labelAuthor.Location = new System.Drawing.Point(339, 121);
            this.labelAuthor.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelAuthor.Name = "labelAuthor";
            this.labelAuthor.Size = new System.Drawing.Size(52, 15);
            this.labelAuthor.TabIndex = 2;
            this.labelAuthor.Text = "作者:";
            // 
            // labelID
            // 
            this.labelID.AutoSize = true;
            this.labelID.Location = new System.Drawing.Point(355, 85);
            this.labelID.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelID.Name = "labelID";
            this.labelID.Size = new System.Drawing.Size(38, 15);
            this.labelID.TabIndex = 1;
            this.labelID.Text = "ID:";
            // 
            // labelTitle
            // 
            this.labelTitle.AutoSize = true;
            this.labelTitle.Location = new System.Drawing.Point(339, 50);
            this.labelTitle.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.labelTitle.Name = "labelTitle";
            this.labelTitle.Size = new System.Drawing.Size(52, 15);
            this.labelTitle.TabIndex = 0;
            this.labelTitle.Text = "书名:";
            // 
            // ReadAPIForm
            // 
            this.AcceptButton = this.btnSearch;
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(747, 619);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.dgvBookInfo);
            this.Controls.Add(this.groupBox1);
            this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
            this.Name = "ReadAPIForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "豆瓣图书阅读器";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dgvBookInfo)).EndInit();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Button btnSearch;
        private System.Windows.Forms.TextBox tbSearch;
        private System.Windows.Forms.DataGridView dgvBookInfo;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label labelSummery;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.Label labelPrice;
        private System.Windows.Forms.Label labelPages;
        private System.Windows.Forms.Label labelBinding;
        private System.Windows.Forms.Label labelPubTime;
        private System.Windows.Forms.Label labelPublisher;
        private System.Windows.Forms.Label labelTrans;
        private System.Windows.Forms.Label labelAuthor;
        private System.Windows.Forms.Label labelID;
        private System.Windows.Forms.Label labelTitle;
        private System.Windows.Forms.Label labelBookPrice;
        private System.Windows.Forms.Label labelBookPages;
        private System.Windows.Forms.Label labelBookBinding;
        private System.Windows.Forms.Label labelBookPublishDate;
        private System.Windows.Forms.Label labelBookPublisher;
        private System.Windows.Forms.Label labelBookTranslator;
        private System.Windows.Forms.Label labelBookAuthor;
        private System.Windows.Forms.Label labelBookID;
        private System.Windows.Forms.Label labelBookName;
        private System.Windows.Forms.Button btnCancle;
        private System.Windows.Forms.RichTextBox richTbSummery;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
        private System.Windows.Forms.DataGridViewLinkColumn Column4;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column5;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column6;
    }

ReadAPIForm.cs

 public partial class ReadAPIForm : Form
    {
        public ReadAPIForm()
        {
            InitializeComponent();
        }

        /// <summary>
        ///     搜索按钮
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            var thread = new Thread(ThreadSearch);
            thread.IsBackground = true;
            thread.Start();
        }

        private void ThreadSearch()
        {
            Invoke(new Action(SearchBooks));
        }

        /// <summary>
        ///     搜索图书信息
        /// </summary>
        private void SearchBooks()
        {
            try
            {
                if (!string.IsNullOrEmpty(tbSearch.Text))
                {
                    dgvBookInfo.Visible = true;
                    var list = new List<Books>();
                    Invoke((MethodInvoker) delegate
                    {
                        var ds = new GetDataSource();
                        JObject reader = JObject.Parse(ds.GetTagsInfo(tbSearch.Text)); //使用Object读json字符串

                        IEnumerable<JToken> temp = from file in reader["books"].Children()
                            select file;
                        foreach (JToken tempFile in temp)
                        {
                            var book = new Books();
                            book.Title = tempFile["title"].ToString().Trim();
                            if (tempFile["author"].ToString().Trim() == "[]")
                            {
                                book.Author = tempFile["author"].ToString().Trim();
                            }
                            else
                            {
                                book.Author = tempFile["author"][0].ToString().Trim();
                            }
                            book.Url = tempFile["url"].ToString().Trim();
                            book.Pages = tempFile["pages"].ToString().Trim();
                            book.Price = tempFile["price"].ToString().Trim();
                            book.Index = list.Count + 1;
                            list.Add(book);
                        }
                    });
                    if (list.Count == 0)
                    {
                        dgvBookInfo.Visible = false;
                        MessageBox.Show("没有查询到相关信息或者查询信息!!");
                    }
                    else
                    {
                        Invoke((MethodInvoker) delegate { dgvBookInfo.DataSource = list; //数据绑定到datagridview   
                        });
                    }
                }
                else
                {
                    MessageBox.Show("请输入信息后在查找!!");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("没有查询到相关信息!!");
            }
        }

        /// <summary>
        ///     点击查询结果列表,获取图书详细信息
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void dgvBookInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dgvBookInfo.Visible = false;
                panel1.Visible = true;
                int index = dgvBookInfo.CurrentRow.Index;
                string temp = dgvBookInfo.Rows[index].Cells[2].Value.ToString();
                var getdata = new GetDataSource();
                JObject reader = JObject.Parse(getdata.GetJsonFile(temp));
                labelBookName.Text = reader["title"].ToString().Trim();
                labelBookID.Text = reader["id"].ToString().Trim();
                labelBookAuthor.Text = "";
                for (int i = 0; i < reader["author"].Count(); i++)
                {
                    if (reader["author"].ToString().Trim() == "[]")
                    {
                        labelBookAuthor.Text = reader["author"].ToString().Trim();
                    }
                    else
                    {
                        labelBookAuthor.Text += reader["author"][i].ToString().Trim();
                    }
                }
                labelBookPublisher.Text = reader["publisher"].ToString().Trim();
                labelBookPublishDate.Text = reader["pubdate"].ToString().Trim();
                labelBookBinding.Text = reader["binding"].ToString().Trim();
                labelBookPages.Text = reader["pages"].ToString().Trim();
                labelBookPrice.Text = reader["price"].ToString().Trim();
                pictureBox2.ImageLocation = reader["images"]["large"].ToString().Trim();
                richTbSummery.Text = reader["summary"].ToString().Trim();
                labelBookTranslator.Text = "";
                for (int i = 0; i < reader["translator"].Count(); i++)
                {
                    if (reader["translator"].ToString().Trim() == "[]")
                    {
                        labelBookTranslator.Text = reader["translator"].ToString().Trim();
                    }
                    else
                    {
                        labelBookTranslator.Text += reader["translator"][0].ToString().Trim();
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("发生异常,请检查!");
            }
        }

        /// <summary>
        ///     返回按钮,隐藏显示的信息
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void btnCan
cle_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            dgvBookInfo.Visible = true;
        }
    }

运行结果如图:

这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值