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

版权声明:本文为博主原创文章,未经博主允许不得转载。    https://blog.csdn.net/liheao/article/details/80597109
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();
    }

}
--------------------- 
作者:liheao 
来源:CSDN 
原文:https://blog.csdn.net/liheao/article/details/80597109 
版权声明:本文为博主原创文章,转载请附上博文链接!

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页