Json字符串查询

来源:互联网 发布:ios开发需要mac吗 编辑:程序博客网 时间:2024/05/18 02:24
using Newtonsoft.Json;using Newtonsoft.Json.Linq;namespace Json查询{    class Program    {        static void Main(string[] args)        {            var input = @"              { ""store"": {                    ""book"": [                       { ""category"": ""reference"",                            ""author"": ""Nigel Rees"",                            ""title"": ""Sayings of the Century"",                            ""price"": 8.95                      },                      { ""category"": ""fiction"",                            ""author"": ""Evelyn Waugh"",                            ""title"": ""Sword of Honour"",                            ""price"": 12.99                      },                      { ""category"": ""fiction"",                            ""author"": ""Herman Melville"",                            ""title"": ""Moby Dick"",                            ""isbn"": ""0-553-21311-3"",                            ""price"": 8.99                      },                      { ""category"": ""fiction"",                            ""author"": ""J. R. R. Tolkien"",                            ""title"": ""The Lord of the Rings"",                            ""isbn"": ""0-395-19395-8""                      }                    ],                    ""bicycle"": {                      ""color"": ""red"",                      ""price"": 19.95                    }              }            }        ";            var json = JObject.Parse(input);            //输出book[*]中category == 'reference'的book            var acme = json.SelectTokens("$.store.book[?(@.category == 'reference')]");            //输出book[*]中price>10的book            var acme2 = json.SelectTokens("$.store.book[?(@.price>10)]");            //输出book[*]中含有isbn元素的book            var acme3 = json.SelectTokens("$.store.book[?(@.isbn)]");            //输出该json中所有price的值            var acme4 = json.SelectTokens("$..price");            //可以提前编辑一个路径,并多次使用它            var stringpath =("$.store.book[*]");            Console.WriteLine(JsonConvert.SerializeObject(acme));            //var context = new JsonPathContext { ValueSystem = new JsonPathContext.BasicValueSystem() };            //var values = context.SelectNodes(json, "$.store.book[*].author").Select(node => node.Value);            //Console.WriteLine(JsonConvert.SerializeObject(values));            Console.ReadKey();        }    }}

多时候我们为了支持模型结构不变,而把一些变化的东西放入到一个JSON类型的字段当中。这样变化的字段就可以放入到同一个字段当中。而且Json的格式又可以很好的支持不同表关联关系。

虽然好处多多,但是查询匹配过滤就显得有些麻烦!

JSON字符串如何解析,如何写查询条件进行过滤呢?

这里有几种好理解的方案:

1.JsonSQL

JsonSQL实现了使用SQL select语句在json数据结构中查询的功能。

例子:

1
jsonsql.query("select * from json.channel.items order by title desc",json);

主页:http://www.trentrichardson.com/jsonsql/

2.JSONPath

JSONPath就像是针对JSON数据结构的XPath。

例子:

1
jsonPath( books, '$..book[(@.length-1)]')

主页:http://goessner.net/articles/JsonPath/

3.linq.js

linq.js——Javascript中的LINQ(译者注:.Net中的概念,见http://msdn.microsoft.com/zh-tw/library/bb397897)

1
2
3
4
5
var queryResult2 = Enumerable.From(jsonArray)
    .Where("$.user.id < 200")
    .OrderBy("$.user.screen_name")
    .Select("$.user.screen_name + ':' + $.text")
    .ToArray();

主页:http://linqjs.codeplex.com/

4.Json.NET

其实就是 “using Newtonsoft.Json;”。在.net中用来进行json序列化和反序列化的操作,原来其也有进行字符串查询的操作。

上面一开始举得例子中使用的就是这一方法。



0 0
原创粉丝点击