6

我正在尝试解析由 KendoUI 网格发送到我的 Web 服务的过滤器参数,并且在说服杰克逊解析这个 JSON 时遇到了一些问题。据我所知,我可以控制 Kendo 发送的参数的格式,但我不知道如何将参数编组为更好的格式,因此它们现在保持不变。

我打算将这些参数转换为 Oracle 数据库的 SQL 查询。

示例 JSON:

{
    "filters":
    [
        {
            "field": "Name",
            "operator": "contains",
            "value": "John"
        },
        {
            "filters": [
                {
                    "field": "Age",
                    "operator": "gt",
                    "value": 20
                },
                {
                    "field": "Age",
                    "operator": "lt",
                    "value": 85
                }
            ],
            "logic", "and"
        },
        {
            "field": "Address",
            "operator": "doesnotcontain",
            "value": "street"
        }
    ],
    "logic": "or"
}

过滤器。爪哇

public class Filters {
    private List<Filter> filters;
    private String logic;
    // accessors/mutators/toString
}

过滤器.java

public class Filter {
    private String field;
    private String operator;
    private String value;
    // accessors/mutators/toString
}

单元测试

public class KendoGridFilterTest {
    private ObjectMapper mapper;

    @Before
    public void before() {
        mapper = new ObjectMapper();
    }

    @Test
    public void jsonParseTest() {
        final String json = "{\"filters\":[{\"field\":\"Name\",\"operator\":\"contains\",\"value\":\"John\"},{filters: [{\"field\":\"Age\",\"operator\": \"eq\",\"value\": 85},{\"field\": \"Age\",\"operator\": \"eq\",\"value\": 85}]\"logic\", \"and\",},{\"field\": \"Address\",\"operator\": \"doesnotcontain\",\"value\": \"street\"}],\"logic\":\"or\"}";
        Filters filters = mapper.readValue(json, Filters.class);
        assertTrue(json.equals(filters.writeValueAsString(filters);
    }
}

错误

com.fasterxml.jackson.databind.UnrecognizedPropertyException:无法识别的字段
    '逻辑'(com.example.Filter)不是市场可忽略的(3个已知属性
    “值”、“字段”、“运算符”)
在 [来源:java.io.StringReader@3bb2b8;行:1,第 76 列](通过参考
    链:com.example.Filters["filters"]->com.example.Filter["logic"]

我也尝试添加@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")到 Filters 类并得到相同的错误。

4

1 回答 1

2

您的过滤器类不正确。它应该扩展过滤器。更正您的单元测试(json 不正确)后,它可以将您的 json 加载到 Filters 对象中。

public class Filter extends Filters {
    private String field;
    private String operator;
    private String value;
    // accessors/mutators/toString
}
于 2014-01-25T15:35:00.877 回答