.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 (jsonUrl.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; }
}
}
原文地址:http://blog.csdn.net/ycwol/article/details/45028005