我想替换 JObject 中的属性名称。我在网上搜索了一些解决方案。发现我们可以从 Newtonsoft 扩展 Rename 功能。
我也找到了扩展方法。重命名功能适用于问题中提到的 JObjects,但不适用于所有。
我的代码是这样的:
class Program
{
static void Main(string[] args)
{
JObject o = JObject.Parse(@"{
'Stores': [
'Lambton Quay',
'Willis Street'
],
'Manufacturers': [
{
'Name': 'Acme Co',
'Products': [
{
'Name': 'Anvil',
'Price': 50
}
]
},
{
'Name': 'Contoso',
'Products': [
{
'Name': 'Elbow Grease',
'Price': 99.95
},
{
'Name': 'Headlight Fluid',
'Price': 4
}
]
}
]
}");
o.Property("Name").Rename("LongName");
Console.WriteLine(o.ToString());
}
}
public static class NewtonsoftExtensions
{
public static void Rename(this JToken token, string newName)
{
if (token == null)
throw new ArgumentNullException("token", "Cannot rename a null token");
JProperty property;
if (token.Type == JTokenType.Property)
{
if (token.Parent == null)
throw new InvalidOperationException("Cannot rename a property with no parent");
property = (JProperty)token;
}
else
{
if (token.Parent == null || token.Parent.Type != JTokenType.Property)
throw new InvalidOperationException("This token's parent is not a JProperty; cannot rename");
property = (JProperty)token.Parent;
}
// Note: to avoid triggering a clone of the existing property's value,
// we need to save a reference to it and then null out property.Value
// before adding the value to the new JProperty.
// Thanks to @dbc for the suggestion.
var existingValue = property.Value;
property.Value = null;
var newProperty = new JProperty(newName, existingValue);
property.Replace(newProperty);
}
}
这给了我错误“无法重命名空指针”。
谁能告诉我我在这里做错了什么。提前谢谢了。
使用此方法查找令牌。
public static class JsonExtensions
{
public static List<JToken> FindTokens(this JToken containerToken, string name)
{
List<JToken> matches = new List<JToken>();
FindTokens(containerToken, name, matches);
return matches;
}
private static void FindTokens(JToken containerToken, string name, List<JToken> matches)
{
if (containerToken.Type == JTokenType.Object)
{
foreach (JProperty child in containerToken.Children<JProperty>())
{
if (child.Name == name)
{
matches.Add(child.Value);
}
FindTokens(child.Value, name, matches);
}
}
else if (containerToken.Type == JTokenType.Array)
{
foreach (JToken child in containerToken.Children())
{
FindTokens(child, name, matches);
}
}
}
}
现在要做剩下的事情,请使用以下代码:
foreach (JToken token in o.FindTokens("Name"))
{
token.Rename("LongName");
}
实际上,在您的情况下,名称位于制造商数组中,但 o.Property("Name") 只会在根级别搜索它,结果为空。
更新
你说你想省略产品名称,你可以用一个小的 If 来做。用这个修改上面的代码。
foreach (JToken token in o.FindTokens("Name"))
{
//Console.WriteLine(token.Path);
if(!token.Path.Contains("Products"))
token.Rename("LongName");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句