【发布时间】:2016-03-24 06:48:05
【问题描述】:
我有一个像{field:'DateToEnd',dir:'asc'}这样的json字符串
我在 c# 中有一个具有以下两个属性的类。
public class SortDescriptor
{
public SortDescriptor()
{
}
public string Field { get; set; }
public string Dir { get; set; }
}
现在我想将此字符串转换为类对象,以便我可以访问属性。
我尝试了以下示例。但这对我不起作用。
JavaScriptSerializer serialize = new JavaScriptSerializer();
SortDescriptor sort = (SortDescriptor)serialize.DeserializeObject(fixSortString);
它给出以下错误:
Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'NF.Common.SortDescriptor'.
谁能告诉我如何做到这一点?
【问题讨论】:
-
从 nuget (newtonsoft.json) 使用 json.net
-
serialize.Deserialize<SortDescriptor>(fixSortString); -
@JeremyThompson 我已经编辑了我的问题。
-
@GeneR 它对我有用。谢谢。
-
这是我的第二个答案
标签: c# json asp.net-mvc serialization