本文整理汇总了C#中Newtonsoft.Json.Linq.JObject.Properties方法的典型用法代码示例。如果您正苦于以下问题:C# JObject.Properties方法的具体用法?C# JObject.Properties怎么用?C# JObject.Properties使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.Linq.JObject
的用法示例。
在下文中一共展示了JObject.Properties方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HasRed
private static bool HasRed(JObject jObject)
{
return jObject.Properties()
.Select(x => x.Value)
.OfType<JValue>()
.Any(x => x.Value<string>() == "red");
}
示例2: GetSum
long GetSum(JArray arr, string avoid) => arr.Sum((dynamic a) => (long)GetSum(a, avoid)); //don't understand
long GetSum(JObject o, string avoid = null) //don't understand
{
bool shouldAvoid = o.Properties()
.Select(a => a.Value).OfType<JValue>()
.Select(v => v.Value).Contains(avoid);
if (shouldAvoid) return 0;
return o.Properties().Sum((dynamic a) => (long)GetSum(a.Value, avoid));
}
示例3: CompareMaps
private static void CompareMaps(JObject expected, JObject actual)
{
var expectedFileds = expected.Properties().Select(f => f.Name).ToList();
var actualFields = actual.Properties().Select(f => f.Name).ToList();
if (!expectedFileds.OrderBy(f => f).SequenceEqual(actualFields.OrderBy(f => f)))
FailDifferentObjects(expectedFileds, actualFields, expected, actual);
foreach (var expectedProperty in expected.Properties())
{
var actualProperty = actual.Property(expectedProperty.Name);
CompareTokens(expectedProperty.Value, actualProperty.Value);
}
}
示例4: CreateEvent
/// <summary>
/// You could implement this with reflection or some other deserialize algorithm.
/// For this sample, we explicitly implemented this.
/// </summary>
/// <param name="eventJson"></param>
/// <returns></returns>
public Event CreateEvent(JObject eventJson)
{
var eventName = eventJson.Properties().First().Name;
var eventBody = eventJson.Properties().First().Value.Value<JObject>();
switch(eventName)
{
case "CQRSMicroservices.Articles.ArticleCreatedEvent":
return new ArticleCreatedEvent
{
ArticleId = Guid.Parse(eventBody["ArticleId"].Value<string>()),
Description = eventBody["Description"].Value<string>(),
Price = decimal.Parse(eventBody["Price"].Value<string>(), CultureInfo.InvariantCulture)
};
case "CQRSMicroservices.Articles.ArticleAvailableEvent":
return new ArticleAvailableEvent
{
ArticleId = Guid.Parse(eventBody["ArticleId"].Value<string>()),
};
case "CQRSMicroservices.Articles.ArticleUnavailableEvent":
return new ArticleUnavailableEvent
{
ArticleId = Guid.Parse(eventBody["ArticleId"].Value<string>()),
};
case "CQRSMicroservices.Articles.ArticleSoldEvent":
return new ArticleSoldEvent
{
ArticleId = Guid.Parse(eventBody["ArticleId"].Value<string>()),
CustomerId = Guid.Parse(eventBody["CustomerId"].Value<string>()),
Price = decimal.Parse(eventBody["Price"].Value<string>(), CultureInfo.InvariantCulture)
};
case "CQRSMicroservices.Customers.CustomerCreatedEvent":
return new CustomerCreatedEvent
{
CustomerId = Guid.Parse(eventBody["CustomerId"].Value<string>()),
Name = eventBody["Name"].Value<string>()
};
default:
throw new EventNotFoundException(eventName);
}
}
示例5: AddLookupData
public static JObject AddLookupData(string environment, JObject o)
{
var props = o.Properties().ToList();
foreach (var p in props)
{
foreach (var k in LookupIdIndex.Keys)
{
if (p.Name == k && p.Value != null && p.Value.ToString() != string.Empty)
{
Table lookupTable = LookupIdIndex[k];
JObject lookupItem = Get(environment, lookupTable.Name, p.Value.ToString());
if (lookupItem != null)
{
if (lookupTable.LookupDescriptor != null)
{
o[lookupTable.Name] = lookupItem[lookupTable.LookupDescriptor];
}
else
{
o[lookupTable.Name] = lookupItem;
}
o.Remove(p.Name);
}
}
}
}
return o;
}
示例6: ParseFieldsData
public static List<IField> ParseFieldsData(JObject fieldsData, CancellationToken cancelToken)
{
if (fieldsData == null)
{
throw new ArgumentNullException();
}
var fields = new List<IField>();
IList<string> propertyNames = fieldsData.Properties().Select(p => p.Name).ToList();
foreach (string fieldId in propertyNames)
{
cancelToken.ThrowIfCancellationRequested();
JObject fieldData = (JObject)fieldsData.GetValue(fieldId);
var name = (string)fieldData.GetValue("Name");
var type = (string)fieldData.GetValue("Type");
var value = (string)fieldData.GetValue("Value");
ScField newField = new ScField(fieldId, name, type, value);
fields.Add(newField);
}
return fields;
}
示例7: ApplyChanges
/// <summary>
/// Patches an existing source <see cref="JObject"/> with (potentially
/// partial) changes from another <see cref="JObject"/>.
/// </summary>
public static void ApplyChanges(this JObject source, JObject changes)
{
// Metadata properties are never applied.
foreach (var changedProp in changes.Properties().Where(prop => !prop.Name.StartsWith("$"))) {
var sourceProp = source.Property (changedProp.Name);
// If source property doesn't exist, add the new one directly.
if (sourceProp == null) {
source.Add (changedProp);
continue;
}
// If the type changed, or if it's not a JObject, just overwrite
// the entire existing property.
if (sourceProp.Value.Type != changedProp.Value.Type ||
sourceProp.Value.Type != JTokenType.Object) {
sourceProp.Value = changedProp.Value;
continue;
}
var sourceObj = (JObject)sourceProp.Value;
var changeObj = (JObject)changedProp.Value;
// Recurse.
ApplyChanges (sourceObj, changeObj);
}
}
示例8: DefineTable
/// <summary>
/// Defines the local table on the store.
/// </summary>
/// <param name="tableName">Name of the local table.</param>
/// <param name="item">An object that represents the structure of the table.</param>
public override void DefineTable(string tableName, JObject item)
{
if (tableName == null)
{
throw new ArgumentNullException("tableName");
}
if (item == null)
{
throw new ArgumentNullException("item");
}
if (this.Initialized)
{
throw new InvalidOperationException("Cannot define a table after the store has been initialized.");
}
// add id if it is not defined
JToken ignored;
if (!item.TryGetValue(MobileServiceSystemColumns.Id, StringComparison.OrdinalIgnoreCase, out ignored))
{
item[MobileServiceSystemColumns.Id] = String.Empty;
}
var tableDefinition = (from property in item.Properties()
let storeType = SqlHelpers.GetStoreType(property.Value.Type, allowNull: false)
select new ColumnDefinition(property.Name, property.Value.Type, storeType))
.ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase);
var sysProperties = GetSystemProperties(item);
this.tableMap.Add(tableName, new TableDefinition(tableDefinition, sysProperties));
}
示例9: RegisterAsync
/// <summary>
/// Register an Installation with particular registrationId and templates
/// </summary>
/// <param name="registrationId">The registrationId to register</param>
/// <param name="templates">JSON with one more templates to register</param>
/// <returns>Task that completes when registration is complete</returns>
public Task RegisterAsync(string registrationId, JObject templates)
{
if (string.IsNullOrWhiteSpace(registrationId))
{
throw new ArgumentNullException("registrationId");
}
JObject installation = new JObject();
installation[PushInstallationProperties.PUSHCHANNEL] = registrationId;
installation[PushInstallationProperties.PLATFORM] = Platform.Instance.PushUtility.GetPlatform();
if (templates != null)
{
JObject templatesWithStringBody = templates;
foreach (JProperty template in templates.Properties())
{
//Notification hub requires template body to be a string.Convert to string from JObject
var templateBody = template.Value["body"];
if (templateBody != null && templateBody.GetType() == typeof(JObject))
{
templatesWithStringBody[template.Name]["body"] = templateBody.ToString();
}
}
installation[PushInstallationProperties.TEMPLATES] = templatesWithStringBody;
}
return this.PushHttpClient.CreateOrUpdateInstallationAsync(installation);
}
示例10: Fields
public Fields(JObject json)
{
fields = new Dictionary<string, string>();
IList<string> keys = json.Properties().Select(p => p.Name).ToList();
foreach (string key in keys)
fields[key] = json[key].ToString();
}
示例11: SetProperties
public void SetProperties(JObject properties)
{
foreach (var property in properties.Properties())
{
this[property.Name] = property.Value;
}
}
示例12: FromJson
/// <summary>
/// 从 JSON 数据中加载
/// </summary>
/// <param name="data">JSON 数据</param>
/// <returns></returns>
public static Item[] FromJson( JObject data )
{
if ( data == null )
return new Item[0];
return data.Properties().Select( p => CreateItem( p ) ).Where( item => item != null ).ToArray();
}
示例13: FindChildByText
private JProperty FindChildByText(string child, JObject obj)
{
var p= obj.Properties().Where(prop => prop.Contains(child)).ToList();
JProperty newChildJObject = new JProperty("text",child);
return newChildJObject;
}
示例14: UpdateBridgeLightsWithJObject
public static void UpdateBridgeLightsWithJObject(Bridge bridge, JObject json)
{
try
{
// Get light keys
IList<string> lightKeys = json.Properties().Select(p => p.Name).ToList();
bridge.LightList.Clear();
bridge.LightCache.Clear();
foreach(var lightId in lightKeys)
{
Light light = new Light();
light.LightId = lightId;
bridge.LightList.Add(light);
bridge.LightCache[lightId] = light;
JObject lightJson = (JObject)json[lightId];
LightFactory.UpdateLightWithJObject(light, lightJson);
}
}
catch(Exception ex)
{
Debug.WriteLine(ex);
}
}
示例15: Example
public void Example()
{
#region Usage
JObject o = new JObject
{
{ "name1", "value1" },
{ "name2", "value2" }
};
foreach (JProperty property in o.Properties())
{
Console.WriteLine(property.Name + " - " + property.Value);
}
// name1 - value1
// name2 - value2
foreach (KeyValuePair<string, JToken> property in o)
{
Console.WriteLine(property.Key + " - " + property.Value);
}
// name1 - value1
// name2 - value2
#endregion
Assert.AreEqual(2, o.Count);
}