如何在asp .net mvc2中反序列化jqgrid多组搜索条件

时间:2012-04-07 09:46:33

标签: asp.net asp.net-mvc asp.net-mvc-2 serialization jqgrid

如果使用多个分组,jqgrid高级搜索可以生成搜索条件,如

{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":"kk"},{"field":"Nimi","op":"cn","data":"kkk"}],"groups":[]}]}

尝试使用

在ASP .NET MVC2中对其进行反序列化
            var serializer = new JavaScriptSerializer();
            var filtersList = serializer.Deserialize<Filter>(_filters);


class Filter
{
    public GroupOp groupOp { get; set; }
    public List<Rule> rules { get; set; }
    public List<Filter> groups { get; set; }
}

class Rule
{
    public string field { get; set; }
    public Operations op { get; set; }
    public string data { get; set; }
}

enum GroupOp
{
    AND,
    OR
}

enum Operations
{
    eq, // "equal"
    ne, // "not equal"
    lt, // "less"
    le, // "less or equal"
    gt, // "greater"
    ge, // "greater or equal"
    bw, // "begins with"
    bn, // "does not begin with"
    @in, // "in"
    ni, // "not in"
    ew, // "ends with"
    en, // "does not end with"
    cn, // "contains"
    nc  // "does not contain"
}

返回空的filtersList.rules属性

如何从这些数据中获取正确的规则?

更新

过滤

    {"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":""},
{"field":"Nimi","op":"cn","data":""},{"field":"Nimi","op":"cn","data":""}],
"groups":[{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":
[{"field":"Nimi","op":"cn","data":""}],"groups":[{"groupOp":"AND","rules":[],
"groups":[{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[],
"groups":[{"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]}]}]}]}]},{"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":""}],"groups":[{"groupOp":"AND","rules":[{"field":"Nimi","op":"cn","data":""},{"field":"Nimi","op":"cn","data":""}],
"groups":[]}]},{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]},{"groupOp":"AND","rules":[],"groups":[]}]}]}]}

使用引用答案中更新部分的代码仍会生成无效的地方

    ((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!')AND(klient.Nimi ILIKE
 ('%' || E'' || '%') ESCAPE '!')AND(klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))AND
((((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))AND(((((())))AND(((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))AND(((klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!')
AND(klient.Nimi ILIKE ('%' || E'' || '%') ESCAPE '!'))))))))

1 个答案:

答案 0 :(得分:1)

我认为代码没有问题

const string filters = "{\"groupOp\":\"AND\",\"rules\":[]," +
    "\"groups\":[{\"groupOp\":\"AND\",\"rules\":[" +
        "{\"field\":\"Nimi\",\"op\":\"cn\",\"data\":\"kk\"}," +
        "{\"field\":\"Nimi\",\"op\":\"cn\",\"data\":\"kkk\"}],\"groups\":[]}]}";

var serializer = new JavaScriptSerializer();
var filtersList = serializer.Deserialize<Filter>(filters);

生成filtersList,其中filtersList.rules为空列表。它对应于您拥有的输入数据。另一方面,filtersList.groups部分不为空。上面代码中的filtersList将生成Filter对象,该对象等效于以下直接初始化:

var filtersList = new Filter {
    groupOp = GroupOp.AND,
    rules = new List<Rule>(0),
    groups = new List<Filter> {
        new Filter {
            groupOp = GroupOp.AND,
            rules = new List<Rule> {
                new Rule {field = "Nimi", op = Operations.cn, data = "kk"},
                new Rule {field = "Nimi", op = Operations.cn, data = "kkk"}
            },
            groups = new List<Filter>(0)
        }
    }
};

如果解析来自filtersList的信息,那么最后仍然可以获得WHERE语句,如下所示

(Nimi LIKE '%kk%') AND (Nimi LIKE '%kkk%')

例如,请参阅the answer的更新部分的代码。准确地说,答案中的代码会生成((Nimi LIKE '%kk%')AND(Nimi LIKE '%kkk%')),这几乎是相同的。