JsonPath for .NET ( http://code.google.com/p/jsonpath/downloads/list ) を使用しようとしていますが、Json 文字列と JsonPath 文字列を解析する方法の例を見つけるのに苦労しています。結果を得る。
誰もこれを使用しましたか?
JsonPath for .NET ( http://code.google.com/p/jsonpath/downloads/list ) を使用しようとしていますが、Json 文字列と JsonPath 文字列を解析する方法の例を見つけるのに苦労しています。結果を得る。
誰もこれを使用しましたか?
あなたが経験している問題は、JsonPath の C# バージョンには Json パーサーが含まれていないため、シリアライゼーションとデシリアライゼーションを処理する別の Json フレームワークで使用する必要があることです。
JsonPath が機能する方法は、IJsonPathValueSystem
解析された Json オブジェクトをトラバースするために呼び出されるインターフェイスを使用することです。JsonPath には、 Json オブジェクトを表すインターフェイスと Json 配列を表すインターフェイスをBasicValueSystem
使用するビルトインが付属しています。IDictionary
IList
BasicValueSystem
C# コレクション初期化子を使用して構築することにより、互換性のある独自の Json オブジェクトを作成できますが、たとえば、Json がリモート サーバーから文字列の形式で入ってくる場合、これはあまり役に立ちません。
したがって、Json 文字列を取得して、それをIDictionary
オブジェクト、IList
配列、およびプリミティブ値のネストされた構造に解析できれば、JsonPath を使用してそれをフィルター処理できます。運が良ければ、優れたシリアライゼーションとデシリアライゼーション機能を備えた Json.NET を使用して、ジョブのその部分を実行できます。
残念ながら、Json.NET は Json 文字列を .NET と互換性のある形式に逆シリアル化しませんBasicValueSystem
。したがって、Json.NET で JsonPath を使用するための最初のタスクは、生成されるオブジェクト、配列、および値をJsonNetValueSystem
実装IJsonPathValueSystem
し、理解するを作成することです。JObject
JArray
JValue
JObject.Parse
したがって、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;
}
}
これら3 つの部分すべてを使用して、サンプルの 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 サンプルに基づいています。
LINQ (.NET 2.0) が嫌いな人のために:
namespace JsonPath
{
public sealed class JsonNetValueSystem : IJsonPathValueSystem
{
public bool HasMember(object value, string member)
{
if (value is Newtonsoft.Json.Linq.JObject)
{
// return (value as JObject).Properties().Any(property => property.Name == member);
foreach (Newtonsoft.Json.Linq.JProperty property in (value as Newtonsoft.Json.Linq.JObject).Properties())
{
if (property.Name == member)
return true;
}
return false;
}
if (value is Newtonsoft.Json.Linq.JArray)
{
int index = ParseInt(member, -1);
return index >= 0 && index < (value as Newtonsoft.Json.Linq.JArray).Count;
}
return false;
}
public object GetMemberValue(object value, string member)
{
if (value is Newtonsoft.Json.Linq.JObject)
{
var memberValue = (value as Newtonsoft.Json.Linq.JObject)[member];
return memberValue;
}
if (value is Newtonsoft.Json.Linq.JArray)
{
int index = ParseInt(member, -1);
return (value as Newtonsoft.Json.Linq.JArray)[index];
}
return null;
}
public System.Collections.IEnumerable GetMembers(object value)
{
System.Collections.Generic.List<string> ls = new System.Collections.Generic.List<string>();
var jobject = value as Newtonsoft.Json.Linq.JObject;
/// return jobject.Properties().Select(property => property.Name);
foreach (Newtonsoft.Json.Linq.JProperty property in jobject.Properties())
{
ls.Add(property.Name);
}
return ls;
}
public bool IsObject(object value)
{
return value is Newtonsoft.Json.Linq.JObject;
}
public bool IsArray(object value)
{
return value is Newtonsoft.Json.Linq.JArray;
}
public bool IsPrimitive(object value)
{
if (value == null)
throw new System.ArgumentNullException("value");
return value is Newtonsoft.Json.Linq.JObject || value is Newtonsoft.Json.Linq.JArray ? false : true;
}
private int ParseInt(string s, int defaultValue)
{
int result;
return int.TryParse(s, out result) ? result : defaultValue;
}
}
}
使用法:
object obj = Newtonsoft.Json.JsonConvert.DeserializeObject(input);
JsonPath.JsonPathContext context = new JsonPath.JsonPathContext { ValueSystem = new JsonPath.JsonNetValueSystem() };
foreach (JsonPath.JsonPathNode node in context.SelectNodes(obj, "$.store.book[*].author"))
{
Console.WriteLine(node.Value);
}
内部の JavaScriptSerializer() は、フィルタリングが必要ない限り問題なく機能することがわかりました。
JsonPath Dataset + Examplesのデータセットと例を使用する
JsonPath 実装のソースはGitHubでした
public static string SelectFromJson(string inputJsonData, string inputJsonPath)
{
// Use the serializer to deserialize the JSON but also to create the snippets
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
dynamic dynJson = serializer.Deserialize<object>(inputJsonData);
var jsonPath = new JsonPath.JsonPathContext();
var jsonResults = jsonPath.Select(dynJson, inputJsonPath);
var valueList = new List<string>();
foreach (var node in jsonResults)
{
if (node is string)
{
valueList.Add(node);
}
else
{
// If the object is too complex then return a list of JSON snippets
valueList.Add(serializer.Serialize(node));
}
}
return String.Join("\n", valueList);
}
関数は次のように使用されます。
var result = SelectFromJson(@"
{ ""store"": {
""book"": [
{ ""category"": ""fiction"",
""author"": ""J. R. R. Tolkien"",
""title"": ""The Lord of the Rings"",
""isbn"": ""0-395-19395-8"",
""price"": 22.99
}
]
}
}", "$.store.book[*].author");