. NET extracts JSON data.
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 = "M ozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;. NET CLR 1.1.4322;. NET CLR 2.0.50727) "; <summary>///create HTTP requests for Get methods///</summary>//<param name= "url" > Request url</p aram>//<param name= "Timeout" > Request time-out </param>//<param name= "useragent" > Browser information </pa ram>//<param name= "encoding" > Coding </param>///<param Name= "headers" > Request header information </param> ; <param name= "Cookies" > Cookie information sent with an HTTP request, if authentication is not required can be null </param>//<RETURNS></RETURNS> ; public static string Get (string url, int? timeout, string useragent, Encoding Encoding, ilist<keyvaluepair<string, S tring>> headers, CookiecolLection cookies = null) {var reponse = creategethttpresponse (URL, timeout, useragent, headers, cookies) ; Return responsetostring (reponse, encoding); }///<summary>//create HTTP requests for Get mode///</summary>//<param name= "url" &G t; requested url</param>//<param name= "Timeout" > timeout for request </param>//<param name= "Userage NT "> client browser information for request, can be null </param>//<param name=" headers "> requested client browser header, can be null </param>// <param name= "Cookies" > Cookie information sent with HTTP requests, if authentication is not required can be null </param>//<returns></returns> public static HttpWebResponse creategethttpresponse (string url, int? timeout, string useragent, ILIST&L T 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 (cookie = null) {request. Cookiecontainer = new Cookiecontainer (); Request. Cookiecontainer.add (cookies); }//If 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) {u Sing (var sr = new StreamReader (stream, encoding)) {result = Sr. ReadToEnd (); }}} reponse. Close (); } return result; } }}
Use:
String jsonurl = "http://www.xxx.cn/api/replymsg?keywords=" + con; Get json/* {"id": 1, "keywords": "A clear picture", "title": "A clear Picture | Second understanding" three mutual "chase construction reform Plan", "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.deserializeobj Ect<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 Weixi NVO;}
Entity class:
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;}} }
. NET extracting JSON data