gpt4 book ai didi

c# - 如何使用使用 JsonPath 的 C# 示例?

转载 作者:可可西里 更新时间:2023-11-01 02:59:30 25 4
gpt4 key购买 nike

我正在尝试将 JsonPath 用于 .NET ( http://code.google.com/p/jsonpath/downloads/list ),但我无法找到有关如何解析 Json 字符串和 JsonPath 字符串并获得结果的示例。

有人用过吗?

最佳答案

您遇到的问题是 JsonPath 的 C# 版本不包含 Json 解析器,因此您必须将它与另一个处理序列化和反序列化的 Json 框架一起使用。

JsonPath 的工作方式是使用一个名为IJsonPathValueSystem 的接口(interface)来遍历已解析的Json 对象。 JsonPath 自带一个内置的 BasicValueSystem,它使用 IDictionary 接口(interface)来表示 Json 对象,使用 IList 接口(interface)来表示 Json 数组。

您可以创建自己的 BasicValueSystem 兼容的 Json 对象,方法是使用 C# 集合初始值设定项构造它们,但是当您的 Json 以字符串形式从远程服务器传入时,这没有多大用处,例如。

因此,如果您可以采用 Json 字符串并将其解析为 IDictionary 对象、IList 数组和原始值的嵌套结构,那么您就可以使用 JsonPath 来过滤它!幸运的是,我们可以使用具有良好序列化和反序列化功能的 Json.NET 来完成这部分工作。

遗憾的是,Json.NET 不会将 Json 字符串反序列化为与 BasicValueSystem 兼容的格式。因此,将 JsonPath 与 Json.NET 一起使用的第一个任务是编写一个 JsonNetValueSystem 来实现 IJsonPathValueSystem 并理解 JObject 对象, JArray 数组和 JObject.Parse 生成的 JValue 值。

因此下载 JsonPath 和 Json.NET 并将它们放入 C# 项目中。然后将此类添加到该项目:

public sealed class JsonNetValueSystem : IJsonPathValueSystem
{
public bool HasMember(object value, 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;
}

public object GetMemberValue(object value, 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(object value)
{
var jobject = value as JObject;
return jobject.Properties().Select(property => property.Name);
}

public bool IsObject(object value)
{
return value is JObject;
}

public bool IsArray(object value)
{
return value is JArray;
}

public bool IsPrimitive(object value)
{
if (value == null)
throw new ArgumentNullException("value");

return value is JObject || value is JArray ? false : true;
}

private int ParseInt(string s, int defaultValue)
{
int result;
return int.TryParse(s, out result) ? result : defaultValue;
}
}

现在有了这些部分的所有三个,我们可以编写一个示例 JsonPath 程序:

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"",
""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"]

此示例基于 JsonPath 站点上的 Javascript 示例:

关于c# - 如何使用使用 JsonPath 的 C# 示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7537398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com