Json字符串查询

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值