如何将 JSON 数组转换为 C# 列表?
How to convert a JSON array into a C# List?
我正在将对象序列化为 JSON 字符串,以便将其存储在 cookie 中。对象看起来像这样:
public class ShoppingCart
{
public List<ShoppingCartItem> Items { get; set; }
public ShoppingCart()
{
Items = new List<ShoppingCartItem>();
}
}
public class ShoppingCartItem
{
public enum ShoppingCartItemType
{
TypeOfItem,
AnotherTypeOfItem
}
public int Identifier { get; set; }
public ShoppingCartItemType Type { get; set; }
}
然后我希望能够从 cookie 中检索对象作为 JSON 字符串,并将其解码回 ShoppingCart
类型的对象,并正确反序列化其项目。
这是我用来执行此操作的代码:
public class CookieStore
{
public static void SetCookie(string key, object value, TimeSpan expires)
{
string valueToStore = Json.Encode(value);
HttpCookie cookie = new HttpCookie(key, valueToStore);
if (HttpContext.Current.Request.Cookies[key] != null)
{
var cookieOld = HttpContext.Current.Request.Cookies[key];
cookieOld.Expires = DateTime.Now.Add(expires);
cookieOld.Value = cookie.Value;
HttpContext.Current.Response.Cookies.Add(cookieOld);
}
else
{
cookie.Expires = DateTime.Now.Add(expires);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
public static object GetCookie(string key)
{
string value = string.Empty;
HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
if (cookie != null)
{
value = cookie.Value;
}
return Json.Decode(value);
}
}
请注意,存储 cookie 效果很好。它的名称正确,JSON 字符串对我来说看起来不错;这是一个例子:
{"Items":[{"Identifier":1,"Type":1}]}
问题是,当我尝试反序列化它时,我认为它没有识别出数组实际上是一个 List<ShoppingCartItem>
,所以我最终得到了 ShoppingCart
[=31 的一个实例=] 并将项目 属性 设置为空 List<ShoppingCartItem>
.
有谁知道我该怎么做?如果可能的话,我更愿意为此继续使用标准 System.Web.Helpers.Json
,但如果需要更强大的 JSON 序列化程序,我愿意这样做。
需要显式转换类型,即
return Json.Decode<ShoppingCart>(value);
之前有人问过类似的问题,使用Newtonsoft JsonConvert.DeserializeObject
works well for the purpose, check the following links. You may even consider Javascriptserializer
How to convert Json array to list of objects in c#
Deserialize JSON array(or list) in C#
deserialize json array list in c#
我正在将对象序列化为 JSON 字符串,以便将其存储在 cookie 中。对象看起来像这样:
public class ShoppingCart
{
public List<ShoppingCartItem> Items { get; set; }
public ShoppingCart()
{
Items = new List<ShoppingCartItem>();
}
}
public class ShoppingCartItem
{
public enum ShoppingCartItemType
{
TypeOfItem,
AnotherTypeOfItem
}
public int Identifier { get; set; }
public ShoppingCartItemType Type { get; set; }
}
然后我希望能够从 cookie 中检索对象作为 JSON 字符串,并将其解码回 ShoppingCart
类型的对象,并正确反序列化其项目。
这是我用来执行此操作的代码:
public class CookieStore
{
public static void SetCookie(string key, object value, TimeSpan expires)
{
string valueToStore = Json.Encode(value);
HttpCookie cookie = new HttpCookie(key, valueToStore);
if (HttpContext.Current.Request.Cookies[key] != null)
{
var cookieOld = HttpContext.Current.Request.Cookies[key];
cookieOld.Expires = DateTime.Now.Add(expires);
cookieOld.Value = cookie.Value;
HttpContext.Current.Response.Cookies.Add(cookieOld);
}
else
{
cookie.Expires = DateTime.Now.Add(expires);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
public static object GetCookie(string key)
{
string value = string.Empty;
HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
if (cookie != null)
{
value = cookie.Value;
}
return Json.Decode(value);
}
}
请注意,存储 cookie 效果很好。它的名称正确,JSON 字符串对我来说看起来不错;这是一个例子:
{"Items":[{"Identifier":1,"Type":1}]}
问题是,当我尝试反序列化它时,我认为它没有识别出数组实际上是一个 List<ShoppingCartItem>
,所以我最终得到了 ShoppingCart
[=31 的一个实例=] 并将项目 属性 设置为空 List<ShoppingCartItem>
.
有谁知道我该怎么做?如果可能的话,我更愿意为此继续使用标准 System.Web.Helpers.Json
,但如果需要更强大的 JSON 序列化程序,我愿意这样做。
需要显式转换类型,即
return Json.Decode<ShoppingCart>(value);
之前有人问过类似的问题,使用Newtonsoft JsonConvert.DeserializeObject
works well for the purpose, check the following links. You may even consider Javascriptserializer
How to convert Json array to list of objects in c#
Deserialize JSON array(or list) in C#
deserialize json array list in c#