代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.NET
】
.net 提取 json 数据
作者:
元青
/ 发布于
2016/12/19
/
531
.net 提取 json 数据. HttpHelper.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Net; using System.Text; using System.IO; namespace Weixin.Common { public class HttpHelper { private const string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; /// <summary> /// 创建GET方式的HTTP请求 /// </summary> /// <param name="url">请求URL</param> /// <param name="timeout">请求超时时间</param> /// <param name="userAgent">浏览器信息</param> /// <param name="encoding">编码</param> /// <param name="headers">请求头信息</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static String Get(string url, int? timeout, string userAgent, Encoding encoding, IList<KeyValuePair<string, string>> headers, CookieCollection cookies = null) { var reponse = CreateGetHttpResponse(url, timeout, userAgent, headers, cookies); return ResponseToString(reponse, encoding); } /// <summary> /// 创建GET方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="headers">请求的客户端浏览器头,可以为空</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, IList<KeyValuePair<string, string>> headers, CookieCollection cookies = null) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } var request = WebRequest.Create(url) as HttpWebRequest; if (request == null) { return null; } request.Method = "GET"; request.UserAgent = DefaultUserAgent; if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } if (timeout.HasValue) { request.Timeout = timeout.Value; } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } //如果Header if (headers != null) { foreach (var header in headers) { request.Headers.Add(header.Key, header.Value); } } return request.GetResponse() as HttpWebResponse; } public static string ResponseToString(WebResponse reponse, Encoding encoding) { var result = string.Empty; if (reponse != null) { using (var stream = reponse.GetResponseStream()) { if (stream != null) { using (var sr = new StreamReader(stream, encoding)) { result = sr.ReadToEnd(); } } } reponse.Close(); } return result; } } } 使用: string jsonUrl = "http://www.xxx.cn/api/replymsg?keywords=" + con; //得到json /* {"id":1,"keywords":"一图了然","title":"一图了然|秒懂“三互”大通关建设改革方案","img":null,"link":"http://mp.weixin.qq.com/s?__***38f#rd","createTime":"\/Date(1428129560757)\/"} */ string tt = HttpHelper.Get(jsonUrl, null, null, Encoding.UTF8, null, null); if (tt.Count() != 0) { twxxInfo twxx = new twxxInfo(); try { twxx = JsonConvert.DeserializeObject<twxxInfo>(tt); } catch (Exception ex) { title = ex.Message; } string title = twxx.title; string description = twxx.description; string img = twxx.img; string link = twxx.link; string[,] kw = { { "" + title + "", "" + description + "", "" + img + "", "" + link + "" } }; weixinVO = getMenuHelper(kw); return weixinVO; } 实体类: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Weixin.Common { public class twxxInfo { public int id { get; set; } public string keywords { get; set; } public string title { get; set; } public string img { get; set; } public string description { get; set; } public string link { get; set; } public Nullable<System.DateTime> createTime { get; set; } } } 若json不是上面的格式,要取其中的某个字段的值 { "code": 0, "data": { "country": "中国", "country_id": "CN", "area": "华南", "area_id": "800000", "region": "广东省", "region_id": "440000", "city": "深圳市", "city_id": "440300", "county": "", "county_id": "-1", "isp": "电信", "isp_id": "100017", "ip": "183.**.**.***" } } 取city的值: if (tt.Count() != 0) { JObject s; try { s = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(tt); } catch (Exception ex) { return null; } string city = s["data"]["city"].ToString(); }
试试其它关键字
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
元青
贡献的其它代码
(
13
)
.
Java 集合转换(Object[]数组、List、Set、Map相互转换
.
返回指定查询结果中,所有记录串接成一个字符串
.
文件相关
.
.net 提取 json 数据
.
#仅复制表结构不导数据
.
利用反射给实体属性赋值
.
Android获得系统时间(24小时制)
.
Js实现Ctrl+S/Ctrl+N/Ctrl+Z提交/撤销/新建/
.
Jquery-鼠标悬停(hover)切换 class 属性
.
判断页面是第一次打开还是刷新
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3