本文讲述了如何使用JsonPath使用C#示例!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:
您遇到的问题是JsonPath的C#版本不包含Json解析器,因此您必须将其与另一个处理序列化和反序列化的Json框架一起使用.
JsonPath的工作方式是使用一个名为IJsonPathValueSystem遍历解析的Json对象的接口.JsonPath附带了一个内置函数,BasicValueSystem它使用IDictionary接口来表示Json对象,并使用IList接口来表示Json数组.
您可以BasicValueSystem通过使用C#集合初始化程序构建它们来创建自己的兼容Json对象,但是当您的Json以远程服务器中的字符串形式进入时,这没什么用处.
因此,如果只有您可以获取Json字符串并将其解析为IDictionary对象,IList数组和原始值的嵌套结构,则可以使用JsonPath对其进行过滤!幸运的是,我们可以使用具有良好序列化和反序列化功能的Json.NET来完成这部分工作.
不幸的是,Json.NET没有将Json字符串反序列化为与BasicValueSystem.兼容的格式.因此,使用JsonPath和Json.NET的第一个任务是编写一个JsonNetValueSystem实现IJsonPathValueSystem并理解生成的JObject对象,JArray数组和JValue值的任务JObject.Parse.
所以下载JsonPath和Json.NET并将它们放入C#项目中.然后将此类添加到该项目:
publicsealed class JsonNetValueSystem : IJsonPathValueSystem { publicboolHasMember(objectvalue, string member) { if (value is JObject) return (value as JObject).Properties().Any(property => property.Name == member); if (value is JArray) { int index = ParseInt(member, -1); return index >= 0 && index < (value as JArray).Count; } return false; } publicobjectGetMemberValue(objectvalue, string member) { if (value is JObject) { var memberValue = (value as JObject)[member]; return memberValue; } if (value is JArray) { int index = ParseInt(member, -1); return (value as JArray)[index]; } return null; } public IEnumerable GetMembers(objectvalue) { var jobject = value as JObject; return jobject.Properties().Select(property => property.Name); } publicboolIsObject(objectvalue) { return value is JObject; } publicboolIsArray(objectvalue) { return value is JArray; } publicboolIsPrimitive(objectvalue) { if (value == null) throw new ArgumentNullException("value"); return value is JObject || value is JArray ? false : true; } privateintParseInt(string s, int defaultValue) { int result; return int.TryParse(s, out result) ? result : defaultValue; } }
现在有了这三个部分,我们可以编写一个示例JsonPath程序:
classProgram { staticvoidMain(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"", ""price"": 22.99 } ], ""bicycle"": { ""color"": ""red"", ""price"": 19.95 } } } "; var json = JObject.Parse(input); var context = new JsonPathContext { ValueSystem = new JsonNetValueSystem() }; var values = context.SelectNodes(json, "$.store.book[*].author").Select(node => node.Value); Console.WriteLine(JsonConvert.SerializeObject(values)); Console.ReadKey(); } }
产生这个输出:
"Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。
想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!