从JSON反序列化HttpCookie对象

时间:2015-08-23 12:42:57

标签: c# json json.net

出于某种原因,HttpCookie对象无法从JSON反序列化。我收到此错误 -

  

无法填充列表类型System.Web.HttpValueCollection。路径'值',行..,位置..

我设法将JSON反序列化为没有HttpCookieModel属性的自定义类(Values),然后从数据重建HttpCookie。 但是,有没有更简单的方法?

using Newtonsoft.Json; //  v7.0.1

public JsonResult GetCookie() {
     return Json(new { Success = true, Username = model.UserName, Cookie = FormsAuthentication.GetAuthCookie(model.UserName, true) });   
}

private static void DoSomeTests()
{
      // HttpWebRequest request....
      // Call GetCookie()
      // ...
      var httpResponse = (HttpWebResponse)request.GetResponse();
      using (var streamReader = new StreamReader(httpResponse.GetResponseStream(), true))
      {
           res = streamReader.ReadToEnd();
      }
      // Deserialize
      try
      {
           MyResponse mr = JsonConvert.DeserializeObject<MyResponse>(res);
      }
      catch (Exception ex)
      {
          string message = ex.Message;   // message: Cannot populate list type System.Web.HttpValueCollection. Path 'Values'....
      }


    public class MyResponse 
    {
        public bool Success { get; set; }
        public string Username { get; set; }
       // public HttpCookie Cookie { get; set; } // Problems deserializing Values collection.
        public HttpCookieModel Cookie { get; set; }
    }

   // This model works - but is there a simpler way?
    public class HttpCookieModel
    {
        public string Domain { get; set; }
        public DateTime Expires { get; set; }
        public bool HasKeys { get; set; }
        public bool HttpOnly { get; set; }
        public string Name { get; set; }
        public string Path { get; set; }
        public bool Secure { get; set; }
        public bool Shareable { get; set; }
        public string Value { get; set; }

        public HttpCookie ConvertToHttpCookie()
        {
            HttpCookie result           = new HttpCookie(this.Name);
            result.Domain               = this.Domain;
            result.Expires              = this.Expires;
            result.HttpOnly             = this.HttpOnly;
            result.Path                 = this.Path;
            result.Secure               = this.Secure;
            result.Shareable            = this.Shareable;
            result.Value                = this.Value;
            return result;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

获取json响应并将其导入http://json2csharp.com/这将创建正确反序列化所需的必要类,然后只需反序列化到RootObject类(RootObject是json2csharp给出的默认名称,将其更改为适合您的需求

答案 1 :(得分:0)

尝试MissingMemberHandling.Ignore

使用示例:

mycommand

使用您的代码:

var jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;

JsonConvert.DeserializeObject<YourClass>(jsonResponse, jsonSerializerSettings);

申请public class MyResponse { public bool Success { get; set; } public string Username { get; set; } public HttpCookie Cookie { get; set; } //use HttpCookie as normal }

MissingMemberHandling.Ignore