本文整理汇总了C#中JToken.SelectTokens方法的典型用法代码示例。如果您正苦于以下问题:C# JToken.SelectTokens方法的具体用法?C# JToken.SelectTokens怎么用?C# JToken.SelectTokens使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JToken
的用法示例。
在下文中一共展示了JToken.SelectTokens方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertPesudoTypes
public static JToken ConvertPesudoTypes(JToken data, FormatOptions fmt)
{
var reqlTypes = data.SelectTokens("$..$reql_type$").ToList();
foreach ( var typeToken in reqlTypes )
{
var reqlType = typeToken.Value<string>();
//JObject -> JProerty -> JVaule:$reql_type$, go backup the chain.
var pesudoObject = typeToken.Parent.Parent as JObject;
JToken convertedValue = null;
if( reqlType == Time )
{
if( fmt.RawTime )
continue;
convertedValue = new JValue(GetTime(pesudoObject));
}
else if( reqlType == GroupedData )
{
if( fmt.RawGroups )
continue;
convertedValue = new JValue(GetGrouped(pesudoObject));
}
else if( reqlType == Binary )
{
if( fmt.RawBinary )
continue;
convertedValue = new JArray(GetBinary(pesudoObject));
}
pesudoObject.Replace(convertedValue);
}
return data;
}
示例2: SelectChildren
/// <summary/>
private static IEnumerable<JObject> SelectChildren(JToken root, string path)
{
// JObject children (only)
// As JObject(fields) have a clear mapping to Row(columns) as opposed to JArray (positional) or JValue(scalar)
// Note:
// We ignore other types (as opposed to fail fast) since JSON supports heterogeneous (schema)
// We support the values that can be mapped, without failing all of them if one of happens to not be an Object.
// Path specified
if(!string.IsNullOrEmpty(path))
{
return root.SelectTokens(path).OfType<JObject>();
}
// Single JObject
var o = root as JObject;
if(o != null)
{
return new []{o};
}
// Multiple JObjects
return root.Children().OfType<JObject>();
}
示例3: Remove
/// <summary>
/// Removes tokens from this JSON token using a collection of patterns.
/// This naive algorithm marks all the affected leaves every time
/// a pattern is evaluated.
/// </summary>
public void Remove(JToken root, PatternCollection patterns)
{
var leaves = ((JContainer) root).DescendantsAndSelf().OfType<JValue>().ToArray();
var exclusionDictionary = leaves.ToDictionary(GetId, x => false);
foreach (var pattern in patterns)
foreach (var matched in root.SelectTokens(pattern.Expression))
foreach (var leaf in FindLeaves(matched))
exclusionDictionary[GetId(leaf)] = !pattern.IsNegated;
var toRemove = leaves.Where(x => exclusionDictionary[GetId(x)]);
var queue = new Queue<JToken>(toRemove);
while (queue.IsNotEmpty())
{
var toHandle = queue.Dequeue();
if (!toHandle.HasValues)
{
var parent = toHandle.RemoveFromParent();
if (parent != null)
queue.Enqueue(parent);
}
}
}
示例4: ParseResults
public List<MovieSearchResult> ParseResults(JToken token)
{
var items = new JArray(token.SelectTokens("[*].results").Cast<JArray>().SelectMany(a => a));
var result = items.ToObject<List<MovieSearchResult>>();
return result;
}
示例5: ParseMovies
public List<Movie> ParseMovies(JToken token)
{
int i = 0;
var items = new JArray(token.SelectTokens("[*].results").Cast<JArray>().SelectMany(a => a));
var result = items.ToObject<List<Movie>>();
result.ForEach(a => a.Source = items[i++].ToString(Formatting.Indented));
return result;
}
示例6: ConvertPseudoTypes
/// <summary>
/// Method for converting pseudo types in JToken (JObjects)
/// </summary>
public static void ConvertPseudoTypes(JToken data, FormatOptions fmt)
{
var reqlTypes = data.SelectTokens("$..$reql_type$").ToList();
foreach (var typeToken in reqlTypes)
{
var reqlType = typeToken.Value<string>();
//JObject -> JProerty -> JVaule:$reql_type$, go backup the chain.
var pesudoObject = typeToken.Parent.Parent as JObject;
JToken convertedValue = null;
if (reqlType == Time)
{
if (fmt.RawTime)
continue;
convertedValue = new JValue(GetTime(pesudoObject));
}
else if (reqlType == GroupedData)
{
if (fmt.RawGroups)
continue;
convertedValue = GetGrouped(pesudoObject);
}
else if (reqlType == Binary)
{
if (fmt.RawBinary)
continue;
convertedValue = new JValue(GetBinary(pesudoObject));
}
else if (reqlType == Geometry)
{
// Nothing specific here
continue;
}
else
{
// Just leave unknown pseudo-types alone
continue;
}
pesudoObject.Replace(convertedValue);
}
}