[英]Deserializing JSON as a Generic List
I have a filter string as shown in the below format: 我有一个过滤字符串,如下面的格式所示:
{"groupOp":"AND","rules":[{"field":"FName","op":"bw","data":"te"}]}
I need to deserialize this as a Generic list of items. 我需要将其反序列化为通用项目列表。
Can anyone guide me on how to do this? 任何人都可以指导我如何做到这一点?
Have a look at JSON.NET . 看看JSON.NET 。 It allows you to do things like: 它允许您执行以下操作:
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
Source: the documentation . 资料来源: 文件 。
Try using the JavaScriptSerializer class like so: 尝试使用JavaScriptSerializer类,如下所示:
Deserialization code 反序列化代码
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);
Classes 类
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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.