protected void Bind() { try { GetJson getJson = new GetJson(); JObject file = JObject.Parse(getJson.GetJsonByUrl(_bookUrl)); this.labelTitle.Text = "《" + file["title"].ToString().Trim() + "》"; this.labelAuthor.Text = file["author"].ToString().Trim() == "[]" ? file["author"].ToString().Trim() : file["author"][0].ToString().Trim(); this.labelCompany.Text = file["publisher"].ToString().Trim(); this.labelYear.Text = file["pubdate"].ToString().Trim(); this.labelPageNum.Text = file["pages"].ToString().Trim(); this.labelISBN.Text = file["isbn10"].ToString().Trim(); this.labelPrice.Text = file["price"].ToString().Trim(); this.pictureBoxBook.ImageLocation = file["image"].ToString().Trim(); this.textBoxIntro.Text = file["summary"].ToString().Trim(); } catch (Exception) { } }
/// <summary> /// 搜索按钮的点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSearch_Click(object sender, EventArgs e) { string tagString = textBoxSearch.Text.Trim(); if (tagString != "输入图书关键字进行查询" && tagString != "") { try { //创建一个table储存读到的数据 DataTable dt = new DataTable(); dt.Columns.Add("序列", typeof(int)); dt.Columns.Add("图书名称", typeof(string)); dt.Columns.Add("作者",typeof(string)); dt.Columns.Add("出版社", typeof(string)); dt.Columns.Add("出版年", typeof(string)); dt.Columns.Add("页数", typeof(string)); dt.Columns.Add("ISBN号", typeof(string)); dt.Columns.Add("售价", typeof(string)); dt.Columns.Add("bookUrl", typeof(string)); int order = 0; string name = ""; string author = ""; string company = ""; string year = ""; string pageNum = ""; string ISBN = ""; string price = ""; string bookUrl = ""; GetJson getJson = new GetJson(); //利用Newtonsoft解析Json JObject jobject = JObject.Parse(getJson.GetJsonByTag(tagString)); var jsonFile = from file in jobject["books"].Children() select file; foreach (var file in jsonFile) { order++;//序列递增 name = file["title"].ToString().Trim(); author = file["author"].ToString().Trim() == "[]" ? file["author"].ToString().Trim() : file["author"][0].ToString().Trim(); company = file["publisher"].ToString().Trim() ; year = file["pubdate"].ToString().Trim(); pageNum = file["pages"].ToString().Trim(); ISBN = file["isbn10"].ToString().Trim() ; price = file["price"].ToString().Trim(); bookUrl = file["url"].ToString().Trim(); //循环向table里面写入数据 dt.Rows.Add(order, name, author, company, year, pageNum, ISBN, price,bookUrl); } if (order == 0) { MessageBox.Show("没有查询到相关书籍!"); } else { //绑定控件 this.dataGridView1.AutoGenerateColumns = false; this.dataGridView1.DataSource = dt; } } catch (Exception) { MessageBox.Show("查询出错,请检查您的网络连接!"); return; } } else { MessageBox.Show("请输入相应的图书关键字!"); return; } }