我有一个过滤字符串,如下所示:
{"groupOp":"AND","rules":[{"field":"FName","op":"bw","data":"te"}]}
我需要将其反序列化为通用项目列表。
任何人都可以指导我如何做到这一点吗?
答案 0 :(得分:2)
看看JSON.NET。它允许您执行以下操作:
JObject o = new JObject(
new JProperty("Name", "John Smith"),
new JProperty("BirthDate", new DateTime(1983, 3, 20))
);
JsonSerializer serializer = new JsonSerializer();
Person p = (Person)serializer.Deserialize(new JTokenReader(o), typeof(Person));
Console.WriteLine(p.Name);
// John Smith
答案 1 :(得分:0)
尝试使用JavaScriptSerializer类,如下所示:
反序列化代码
using System.Web.Script.Serialization;
...
string json = "{\"groupOp\":\"AND\",\"rules\":[{\"field\":\"FName\",\"op\":\"bw\",\"data\":\"te\"}]}";
JavaScriptSerializer serializer = new JavaScriptSerializer();
Filter filter = (Filter)serializer.Deserialize<Filter>(json);
<强>类强>
public class Filter
{
public string GroupOp { get; set; }
public List<Rule> Rules { get; set; }
public Filter()
{
Rules = new List<Rule>();
}
}
public class Rule
{
public string Field { get; set; }
public string Op { get; set; }
public string Data { get; set; }
}