zoukankan      html  css  js  c++  java
  • C# 根据url返回JObject

            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;

  • 相关阅读:
    用OLEDB读取EXCEL时,单元格内容长度超过255被截断
    jquery对属性和特性的操作
    yum报[Errno 256] No more mirrors to try
    c语言:计算数组长度
    递归之100.相同的树
    1160.Find Words That Can Be Formed By Characters
    892.surface area of 3D shapes
    递归之24&206(链表)
    C++ private
    Dell 7559 安装黑苹果 Mojave14.15.6
  • 原文地址:https://www.cnblogs.com/duhaoran/p/14461908.html
Copyright © 2011-2022 走看看