c#获取json数据并分解(源代码示例)

1.找准链接,根据这个链接获取json数据
https://api.douban.com/v2/book/isbn/9787115212948

其中 9787115212948是书号,真正的地址是 https://api.douban.com/v2/book/isbn/ 书号
获取json格式的内容(你也试试)

{"rating":{"max":10,"numRaters":1,"average":"0.0","min":0},"subtitle":"","author":[],"pubdate":"2009-10","tags":[{"count":1,"name":"sql","title":"sql"}],"origin_title":"","image":"https://img3.doubanio.com\/view\/subject\/m\/public\/s6488014.jpg","binding":"","translator":[],"catalog":"","pages":"430","images":{"small":"https://img3.doubanio.com\/view\/subject\/s\/public\/s6488014.jpg","large":"https://img3.doubanio.com\/view\/subject\/l\/public\/s6488014.jpg","medium":"https://img3.doubanio.com\/view\/subject\/m\/public\/s6488014.jpg"},"alt":"https:\/\/book.douban.com\/subject\/4089806\/","id":"4089806","publisher":"","isbn10":"7115212945","isbn13":"9787115212948","title":"SQL范例完全自学手册","url":"https:\/\/api.douban.com\/v2\/book\/4089806","alt_title":"","author_intro":"","summary":"《SQL范例完全自学手册》是一本集查询、使用、学习和练习为一体的自学手册,书中介绍了应用SQL进行开发的各种技术和技巧。全书分为17章,内容包括SQL基本查询,WHERE子句过滤,ORDERBY子句排序,数值查询,字符串查询,日期查询,聚合数据检索,分组统计数据,子查询,交叉表、递归查询与分布式查询,插入、更新和删除数据,XML数据检索,优化操作,管理和维护数据库、SQL编程与应用等。《SQL范例完全自学手册》精选了154个典型实例,所选实例覆盖了SQL数据库开发中的热点问题和关键问题。全书按实际应用进行分类,可以使读者在短时间内掌握更多实用技术,快速提高编程水平。\n《SQL范例完全自学手册》附有配套DVD光盘,光盘中提供了实例的全程视频讲解及所有实例源代码,这些源代码都经过精心调试,在WindowsXP、Windows2000和Windows2003下测试通过。\n《SQL范例完全自学手册》内容详尽,实例丰富,非常适合作为编程人员及项目开发人员的学习用书。","price":"59.80元"}

2.下载组件
https://download.csdn.net/download/dada52020/2693244
Newtonsoft.Json.dll这个组件是必须的
3.添加引用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
其中1: using Newtonsoft.Json.Linq 适用JObject obj = (JObject)JsonConvert.DeserializeObject(result1)中的JObject;2:using Newtonsoft.Json;适用Object obj = (JObject)JsonConvert.DeserializeObject(result1)中的JsonConvert

4.源代码Default2.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         string result1 = DoPost("https://api.douban.com/v2/book/isbn/9787115212948", "100");
        //取大字符串,这个“100”是我随便填的!!!
        JObject obj = (JObject)JsonConvert.DeserializeObject(result1);
        //再讲字符串转成json格式

        TextBox1.Text = obj["summary"].ToString();
        //获取summary内容
    }
    public string DoPost(string url, string data)
    {
        HttpWebRequest req = GetWebRequest(url, "POST");
        byte[] postData = Encoding.UTF8.GetBytes(data);
        Stream reqStream = req.GetRequestStream();
        reqStream.Write(postData, 0, postData.Length);
        reqStream.Close();
        HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
        Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
        return GetResponseAsString(rsp, encoding);
    }
    public HttpWebRequest GetWebRequest(string url, string method)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.ServicePoint.Expect100Continue = false;
        req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
        req.ContentType = "text/json";
        req.Method = method;
        req.KeepAlive = true;
        req.UserAgent = "guanyisoft";
        req.Timeout = 1000000;
        req.Proxy = null;
        return req;
    }
    public string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
    {
        StringBuilder result = new StringBuilder();
        Stream stream = null;
        StreamReader reader = null;
        try
        {
            // 以字符流的方式读取HTTP响应
            stream = rsp.GetResponseStream();
            reader = new StreamReader(stream, encoding);
            // 每次读取不大于256个字符,并写入字符串
            char[] buffer = new char[256];
            int readBytes = 0;
            while ((readBytes = reader.Read(buffer, 0, buffer.Length)) > 0)
            {
                result.Append(buffer, 0, readBytes);
            }
        }
        finally
        {
            // 释放资源
            if (reader != null) reader.Close();
            if (stream != null) stream.Close();
            if (rsp != null) rsp.Close();
        }
        return result.ToString();
    }

}

5.执行效果


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT技术与企业应用结合的爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值