zoukankan      html  css  js  c++  java
  • json 序列化的两种方式

    JavaScriptSerializer Serializer = new JavaScriptSerializer();
    ResultData<EUserData> resultModel_Login = Serializer.Deserialize<ResultData<EUserData>>(result_Login);
    
    
    
    /// <summary>
    /// Get方法
    /// </summary>
    /// <param name="serverUrl">url地址</param>
    /// <param name="postData">数据 格式为"aa=xx&bb=xxx"</param>
    /// <returns>请求结果</returns>
    public static string HttpGetConnectToServer(string serverUrl, string postData)
    {
    //创建请求 
    var request = (HttpWebRequest)HttpWebRequest.Create(serverUrl + "?" + postData);
    request.Method = "GET";
    //设置上传服务的数据格式 
    request.ContentType = "application/x-www-form-urlencoded";
    //请求的身份验证信息为默认 
    request.Credentials = CredentialCache.DefaultCredentials;
    //请求超时时间 
    request.Timeout = 10000;
    //读取返回消息 
    string res = "";
    try
    {
    var response = (HttpWebResponse)request.GetResponse();
    var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    res = reader.ReadToEnd();
    reader.Close();
    }
    catch (Exception ex)
    {
    
    }
    return res;
    }
    
    /// <summary>
    /// post方法
    /// </summary>
    /// <param name="postUrl">url地址</param>
    /// <param name="paramData">提交的ParamData对象</param>
    /// <returns></returns>
    public static string PostWebRequest<T>(string postUrl, T paramData)
    {
    string ret = string.Empty;
    try
    {
    string str_Tem = JsonSerializer(paramData);//将对象转成json串
    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(str_Tem);
    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
    webReq.Method = "POST";
    webReq.ContentType = " application/json; charset=utf-8";
    webReq.ContentLength = byteArray.Length;
    Stream newStream = webReq.GetRequestStream();
    newStream.Write(byteArray, 0, byteArray.Length);//写入参数
    newStream.Close();
    HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    ret = sr.ReadToEnd();
    sr.Close();
    response.Close();
    newStream.Close();
    }
    catch (Exception ex)
    {
    
    }
    return ret;
    }
    
    
    /// <summary>
    /// C#反序列化JSON
    /// </summary>
    /// <typeparam name="T">返回类型</typeparam>
    /// <param name="jsonString">JSON字符串</param>
    /// <returns></returns>
    public static T JsonDeserialize<T>(string strJson)
    {
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson));
    T objT = (T)ser.ReadObject(ms);
    ms.Close();
    return objT;
    }
    
    /// <summary>
    /// C#序列化JSON
    /// </summary>
    /// <typeparam name="T">序列化对象类型</typeparam>
    /// <param name="objT">序列化对象</param>
    /// <returns></returns>
    public static string JsonSerializer<T>(T objT)
    {
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
    MemoryStream ms = new MemoryStream();
    ser.WriteObject(ms, objT);
    string jsonString = Encoding.UTF8.GetString(ms.ToArray());
    ms.Close();
    return jsonString;
    }
  • 相关阅读:
    Js特殊字符转义之htmlEscape()方法
    利用Image对象,建立Javascript前台错误日志记录
    html5 实现 文件夹上传
    博客园博客开通了
    js 无刷新文件上传 (兼容IE9 )
    js实现关键词高亮显示 正则匹配
    Python练习六
    Python练习二
    Python练习三
    Python练习四
  • 原文地址:https://www.cnblogs.com/muxueyuan/p/4462593.html
Copyright © 2011-2022 走看看