public JObject GetHttpWebRequest(string url) { try { string result = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; byte[] data = Encoding.UTF8.GetBytes(result);//把字符串转换为字节 req.ContentLength = data.Length; //请求长度 using (Stream reqStream = req.GetRequestStream()) //获取 { reqStream.Write(data, 0, data.Length);//向当前流中写入字节 reqStream.Close(); //关闭当前流 } HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); //响应结果 Stream stream = resp.GetResponseStream(); //获取响应内容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } JObject jo = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText); return jo; } catch (Exception) { throw; } }
JObject jo = GetHttpWebRequest(url); string zj1 = jo["data"].ToString(); JavaScriptSerializer Serializers = new JavaScriptSerializer();
//实例化类 ProcessingList pro = new ProcessingList(); //json字符串转为数组对象, 反序列化 objs = Serializers.Deserialize<List<Class>>(zj1);
注意:项目添加:System.Web.Extensions.dll的引用
using System.Xml.Linq;
using System.Web.Script.Serialization;