将 json 反序列化为 dynamic/anonymous class asp.net
Deserialize json to dynamic/anonymous class asp.net
我有一个名为 ShipFromAddress
的具体 class 并且我正在反序列化我的 json 下面的方式
JavaScriptSerializer jss = new JavaScriptSerializer();
oShipFromAddress = jss.Deserialize<ShipFromAddress>(Request.Cookies["ShipFromAddress"].Value);
混凝土class
public class ShipFromAddress
{
public string Weight
{
get;
set;
}
public string addressLine1
{
get;
set;
}
public string addressLine2
{
get;
set;
}
public string city
{
get;
set;
}
public string postcode
{
get;
set;
}
public string countrycode
{
get;
set;
}
public string StateCode
{
get;
set;
}
}
我不想创建或使用具体的 class,而是想借助动态对象或匿名 class 概念即时进行反序列化。请用示例代码指导我。
我有两个解决方案.....看起来不错
1) 当需要将多个数据序列化为匿名时,示例为
var query = from employee in employees select new { Name = employee.Name, Id = employee.Id };
LogEmployees(query);
public void LogEmployees (IEnumerable<dynamic> list)
{
foreach (dynamic item in list)
{
string name = item.Name;
int id = item.Id;
}
}
方法参数类型必须是 IEnumerable<dynamic> because LogEmployees()
需要多个数据的函数
2) 当传递单个数据时代码看起来像
public class Program
{
private static void Thing(dynamic other)
{
Console.WriteLine(other.TheThing);
}
private static void Main()
{
var things = new { TheThing = "Worked!" };
Thing(things);
}
}
借助 JavaScriptSerializer,您可以使用序列化程序中的 DeserializeObject
方法,它将 return 只是一个对象:
JavaScriptSerializer jss = new JavaScriptSerializer();
object obj= jss.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);
在内部它将表示为 Dictionary<string, object>
,因此您可以将其转换为它并像这样使用:
var values = (Dictionary<string, object>)jss.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);
var addressLine1 = values["addressLine1"].ToString();
或者您可以将其转换为 dynamic
:
dynamic values = jss.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);
var addressLine1 = values["addressLine1"].ToString();
或者,你可以使用Json.NET
库,它是JsonConvert
class(基准测试表明它比JavaScriptSerializer
).代码将如下所示:
dynamic values = JsonConvert.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);
var addressLine1 = values.addressLine1;
我用Newtonsoft.Json
string source = Request.Cookies["ShipFromAddress"].Value as string;
var address = JsonConvert.DeserializeObject<ShipFromAddress>(source);
我有一个名为 ShipFromAddress
的具体 class 并且我正在反序列化我的 json 下面的方式
JavaScriptSerializer jss = new JavaScriptSerializer();
oShipFromAddress = jss.Deserialize<ShipFromAddress>(Request.Cookies["ShipFromAddress"].Value);
混凝土class
public class ShipFromAddress
{
public string Weight
{
get;
set;
}
public string addressLine1
{
get;
set;
}
public string addressLine2
{
get;
set;
}
public string city
{
get;
set;
}
public string postcode
{
get;
set;
}
public string countrycode
{
get;
set;
}
public string StateCode
{
get;
set;
}
}
我不想创建或使用具体的 class,而是想借助动态对象或匿名 class 概念即时进行反序列化。请用示例代码指导我。
我有两个解决方案.....看起来不错
1) 当需要将多个数据序列化为匿名时,示例为
var query = from employee in employees select new { Name = employee.Name, Id = employee.Id };
LogEmployees(query);
public void LogEmployees (IEnumerable<dynamic> list)
{
foreach (dynamic item in list)
{
string name = item.Name;
int id = item.Id;
}
}
方法参数类型必须是 IEnumerable<dynamic> because LogEmployees()
需要多个数据的函数
2) 当传递单个数据时代码看起来像
public class Program
{
private static void Thing(dynamic other)
{
Console.WriteLine(other.TheThing);
}
private static void Main()
{
var things = new { TheThing = "Worked!" };
Thing(things);
}
}
借助 JavaScriptSerializer,您可以使用序列化程序中的 DeserializeObject
方法,它将 return 只是一个对象:
JavaScriptSerializer jss = new JavaScriptSerializer();
object obj= jss.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);
在内部它将表示为 Dictionary<string, object>
,因此您可以将其转换为它并像这样使用:
var values = (Dictionary<string, object>)jss.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);
var addressLine1 = values["addressLine1"].ToString();
或者您可以将其转换为 dynamic
:
dynamic values = jss.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);
var addressLine1 = values["addressLine1"].ToString();
或者,你可以使用Json.NET
库,它是JsonConvert
class(基准测试表明它比JavaScriptSerializer
).代码将如下所示:
dynamic values = JsonConvert.DeserializeObject(Request.Cookies["ShipFromAddress"].Value);
var addressLine1 = values.addressLine1;
我用Newtonsoft.Json
string source = Request.Cookies["ShipFromAddress"].Value as string;
var address = JsonConvert.DeserializeObject<ShipFromAddress>(source);