我正在尝试使用 JsonConvert.DeserializeObject(string) 将字符串反序列化为可与动态一起使用的 jobject 来动态访问 json 文档。但是我想避免知道文档的大小写,以便我可以输入
dynamic document = JsonConvert.DeserializeObject(someString);
Console.WriteLine(document.some.path.here.name);
并让它在 {"Some":{"path":{"HERE":{"Name":"test"}}}
上工作
我不知道如何为 json.net 创建一个自定义类来为我做到这一点,基本上消除了 jobject 上的大小写敏感性(或者可能将所有属性转换为小写)
最佳答案
要将 JToken
层次结构中的所有属性递归转换为小写,您可以使用以下扩展方法:
public static class JsonExtensions
{
public static TJToken RenamePropertiesToLowerInvariant<TJToken>(this TJToken root) where TJToken : JToken
{
return root.RenameProperties(s => s.ToLowerInvariant());
}
public static TJToken RenameProperties<TJToken>(this TJToken root, Func<string, string> map) where TJToken : JToken
{
if (map == null)
throw new ArgumentNullException();
if (root == null)
return null;
if (root is JProperty)
{
return RenameReplaceProperty(root as JProperty, map, -1) as TJToken;
}
else
{
foreach (IList<JToken> obj in root.DescendantsAndSelf().OfType<JObject>())
for (int i = obj.Count - 1; i >= 0; i--)
RenameReplaceProperty((JProperty)obj[i], map, i);
return root;
}
}
public static IEnumerable<JToken> DescendantsAndSelf(this JToken node)
{
if (node == null)
return Enumerable.Empty<JToken>();
var container = node as JContainer;
if (container != null)
return container.DescendantsAndSelf();
else
return new[] { node };
}
private static JProperty RenameReplaceProperty(JProperty property, Func<string, string> map, int index)
{
// JProperty.Name is read only so it will need to be replaced in its parent.
if (property == null)
return null;
var newName = map(property.Name);
if (newName == property.Name)
return property;
var value = property.Value;
// Setting property.Value to null on the old property prevents the child JToken hierarchy from getting recursively cloned when added to the new JProperty
// See https://github.com/JamesNK/Newtonsoft.Json/issues/633#issuecomment-133358110
property.Value = null;
var newProperty = new JProperty(newName, value);
IList<JToken> container = property.Parent;
if (container != null)
{
if (index < 0)
index = container.IndexOf(property);
container[index] = newProperty;
}
return newProperty;
}
}
然后做
dynamic document = JsonConvert.DeserializeObject<JToken>(someString).RenamePropertiesToLowerInvariant();
但是,JObject
区分大小写,并且不提供构造函数来在其 JPropertyKeyedCollection
中使用大小写不变的比较器。 。和JObjectDynamicProxy.TryGetMember()
似乎是在进行简单的查找,而不是不区分大小写的搜索。
所以,除非你能得到 this answer为了工作,如果您需要一个不区分大小写的动态对象,您可以从 How to set ExpandoObject's dictionary as case insensitive? 获取 ExpandoObject
的替代品之一然后创建您自己的 ExpandoObjectConverter
版本反序列化您的替代扩展类型。
关于c# - json.net自定义jobject反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42180161/