如何在C#例子中使用JsonPath?我正在尝试将JsonPath用于.NET(http://code.google.com/p/jsonpath/downloads/list),但找不到如何解析Json字符串和JsonPath字符串以获得结果。有人用过这个吗?您遇到的问题是JsonPath的C#版本不包含Json解析器,因此您必须将它与另一个处理序列化和反序列化的Json框架一起使用。JsonPath的工作方式是使用一个名为IJsonPathValueSystem的接口来迭代已解析的Json对象。JsonPath带有内置的BasicValueSystem,它使用IDictionary接口表示Json对象,使用IList接口表示Json数组。您可以通过使用C#集合初始值设定项来构建自己的BasicValueSystem兼容Json对象,但是当您的Json作为字符串从远程服务器传入时,这不是很有用。所以只要你能把一个Json字符串解析成IDictionary对象、IList数组和原始值的嵌套结构,那么你就可以使用JsonPath来过滤它!幸运的是,我们可以使用具有良好序列化和反序列化功能的Json.NET来完成这部分工作。遗憾的是,Json.NET不会将Json字符串反序列化为与BasicValueSystem兼容的格式。所以在Json.NET中使用JsonPath的首要任务是编写一个IJsonPathValueSystem的实现,它理解JObject.Parse生成的JObject对象、JArray数组和JValue值。所以,下载JsonPath和Json.NET并将它们放在C#项目中。然后将此类添加到项目中:publicsealedclassJsonNetValueSystem:IJsonPathValueSystem{publicboolHasMember(objectvalue,stringmember){if(valueisJObject)return(valueasJObject).Properties().Any(property=>property.姓名==成员);if(valueisJArray){intindex=ParseInt(member,-1);返回索引>=0&&索引property.Name);}publicboolIsObject(objectvalue){返回值为JObject;}publicboolIsArray(objectvalue){返回值为JArray;}publicboolIsPrimitive(objectvalue){if(value==null)thrownewArgumentNullException("value");返回值为JObject||值是JArray吗?假:真;}privateintParseInt(strings,intdefaultValue){intresult;返回int.TryParse(s,outresult)?结果:默认值;现在有了这三个部分,我们就可以编写一个示例JsonPath程序了:"category"":""reference",""author"":""NigelRees"",""title"":""SayingsoftheCentury"",""price"":8.95},{""category"":""fiction",""author"":""EvelynWaugh"",""title"":""荣誉之剑"",""price"":12.99},{""category"":""fiction",""author"":""HermanMelville"",""title"":""MobyDick"",""isbn"":""0-553-21311-3",""price"":8.99},{""category"":""fiction"",""author"":""JRRTolkien"",""title"":""指环王",""isbn"":""0-395-19395-8"",""price"":22.99}],""bicycle"":{""color"":""red"",""price"":19.95}}}";varjson=JObject.Parse(input);varcontext=newJsonPathContext{ValueSystem=newJsonNetValueSystem()};varvalues=context.SelectNodes(json,"$.store.book[*].author").Select(node=>node.Value);Console.WriteLine(JsonConvert.SerializeObject(values));Console.ReadKey();}}生成此输出:["NigelRees","EvelynWaugh","HermanMelville","JRRTolkien"]此示例基于JsonPath站点上的Javascript示例:对于那些不喜欢LINQ(.NET2.0)的人:namespaceJsonPath{publicsealedclassJsonNetValueSystem:IJsonPathValueSystem{publicboolHasMember(对象值,字符串成员){if(valueisNewtonsoft.Json.Linq.JObject){//return(valueasJObject).Properties().Any(property=>property.Name==成员);foreach(Newtonsoft.Json.Linq.JPropertypropertyin(valueasNewtonsoft.Json.Linq.JObject).Properties()){if(property.Name==member)返回真;}返回假;}如果(值为Newtonsoft.Json.Linq.JArray){intindex=ParseInt(member,-1);返回索引>=0&&索引ls=newSystem.Collections.Generic.List();varjobject=valueasNewtonsoft.Json.Linq.JObject;///returnjobject.Properties().Select(property=>property.Name);foreach(Newtonsoft.Json.Linq.JPropertypropertyinjobject.Properties()){ls.Add(property.Name);}返回ls;}publicboolIsObject(objectvalue){返回值为Newtonsoft.Json.Linq.JObject;}publicboolIsArray(objectvalue){返回值为Newtonsoft.Json.Linq.JArray;}公共布尔IsPrimitive(objectvalue){if(value==null)thrownewSystem.ArgumentNullException("value");返回值为Newtonsoft.Json.Linq.JObject||值是Newtonsoft.Json.Linq.JArray吗?假:真;}privateintParseInt(strings,intdefaultValue){intresult;返回int.TryParse(s,outresult)?结果:默认值;}}}用法:以上是C#学习教程:HowtouseJsonPathusingC#example?如果分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注——objectobj=Newtonsoft.Json.JsonConvert.DeserializeObject(input);JsonPath.JsonPathContextcontext=newJsonPath.JsonPathContext{ValueSystem=newJsonPath.JsonNetValueSystem()};foreach(JsonPath.JsonPathNodenodeincontext.SelectNodes(obj,"$.store.book[*].author")){Console.WriteLine(node.Value);}网络收藏不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
