示例#1
0
        public void Extract2()
        {
            var schema = JSchema.Parse(schemaJson);

            var keyValues = new[]
            {
                new KeyValuePair <string, StringValues>("name", new [] { "Name" }),
                new KeyValuePair <string, StringValues>("array1[0].key", new [] { "100" }),
                new KeyValuePair <string, StringValues>("array1[0].values[]", new [] { "Val1", "Val2", "Val3" }),
                new KeyValuePair <string, StringValues>("array1[1].key", new [] { "101" }),
                new KeyValuePair <string, StringValues>("array1[1].values[]", new [] { "Val1", "Val2", "Val3" }),
            };

            var token = JTokenBinder.Extract(keyValues, schema);

            Assert.Equal("Name", (string)token["name"]);
            Assert.Equal(2, token["array1"].Cast <IEnumerable <object> >().Count());
            Assert.Equal(100, (long)token.SelectToken("$.array1[0].key"));
            Assert.Equal("Val1", (string)token.SelectToken("$.array1[0].values[0]"));
            Assert.Equal("Val2", (string)token.SelectToken("$.array1[0].values[1]"));
            Assert.Equal("Val3", (string)token.SelectToken("$.array1[0].values[2]"));
            Assert.Equal(101, (long)token.SelectToken("$.array1[1].key"));
            Assert.Equal("Val1", (string)token.SelectToken("$.array1[1].values[0]"));
            Assert.Equal("Val2", (string)token.SelectToken("$.array1[1].values[1]"));
            Assert.Equal("Val3", (string)token.SelectToken("$.array1[1].values[2]"));
        }
示例#2
0
 public static JToken ToJToken(this IFormCollection form, JSchema schema)
 {
     return(JTokenBinder.Extract(form.AsEnumerable(), schema));
 }