【发布时间】:2018-04-26 07:47:52
【问题描述】:
我有以下属性
{
"bad":
{
"Login": "someLogin",
"Street": "someStreet",
"House": "1",
"Flat": "0",
"LastIndication":
[
"230",
"236"
],
"CurrentIndication":
[
"263",
"273"
],
"Photo":
[
null,
null
]
}
}
例如,我如何将其从“坏”重命名为“好”。是的,我看到了 Abi Bellamkonda 的扩展方法
public static class NewtonsoftExtensions
{
public static void Rename(this JToken token, string newName)
{
var parent = token.Parent;
if (parent == null)
throw new InvalidOperationException("The parent is missing.");
var newToken = new JProperty(newName, token);
parent.Replace(newToken);
}
}
但它得到了这个例外
无法将 Newtonsoft.Json.Linq.JProperty 添加到 Newtonsoft.Json.Linq.JProperty.
【问题讨论】:
-
看起来很酷,但它不起作用。在这个例子中,root obj 是它的 JObject。但就我而言,我有一些 JProperty 数组并在 foreach 中使用它们,就像
JArray curLog = JArray.Parse(currentJsonLog); curLog.Children<JObject>().ToList() .ForEach(o => o.Properties().ToList() .ForEach(p => { if (p.Name == "bad") { //p.ChangeKey("newName");}