c# - JSON.Net无法在自定义JsonConverter中反序列化json数组

标签 c# arrays json serialization json.net

我遇到了一个似乎无法解决的令人困惑的问题。

我正在使用 Json.Net,并且编写了一个自定义 Json 转换器来处理我的应用程序中的特殊情况。 我遇到的问题是在 deserialize 或 ReadJson 方法中,当它厌倦将 JSON 数组转换为字符串数组时会抛出错误:

enter image description here

错误的确切文本是:反序列化对象时出现意外标记:PropertyName。路径“RootPages”,第 1 行,位置 13。

正如您从检查器中看到的,它尝试反序列化的 JProperty (RootPages) 已被正确解析并且是有效的 JSON。 enter image description here

所以我不完全确定这里发生了什么,任何启发将不胜感激..

如果相关,原始 JSON 字符串如下:

{
  "RootPages": [
    "TestItem1",
    "TestItem2"
  ],
  "Name": "root"
}

编辑代码:

ReadJson方法:

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
        if (PartialChildPageSerialization) {
            var jsonObject = JObject.Load(reader);
            var properties = jsonObject.Properties().ToList();

            foreach (var property in objectType.GetProperties()) {
                var type = property.PropertyType;
                object value = null;

                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ChildPageCollection<>)) {
                    var collection = Activator.CreateInstance(type);
                    var deserializedValue = properties.First(p => p.Name == property.Name).ToObject<string[]>();
                    type.GetMethod("PolulateFromSerializer").Invoke(collection, new object[] {deserializedValue});
                    value = collection;
                }
                else {
                    value = properties.First(p => p.Name == property.Name).ToObject(type);
                }

                property.SetValue(existingValue, value);
            }

            return existingValue;
        }

        return serializer.Deserialize(reader, objectType);
    }

ChildPageCollection 有趣部分的片段:

public class ChildPageCollection<T> : IList<T> where T : DataPage
{
    public string[] GetNames() => _internalNameList.ToArray();

    internal void PolulateFromSerializer(string[] names) {
        this.Clear();
        _internalNameList.AddRange(names);
        _hasFullList = false;
    }

    private void CheckFullList() {
        if(!_hasFullList)
            throw new InvalidOperationException("Collection has not been fully loaded, and full list is not avialable.");
    }

    private readonly List<T> _internalList = new List<T>();
    private readonly List<string> _internalNameList = new List<string>();
    private bool _hasFullList = true;

    ...
}

最佳答案

我希望这是因为第一个属性是一个对象,其属性名为“RootPages”,它是一个字符串[]。

不幸的是,从屏幕截图的外观来看,您正在尝试将对象转换为字符串数组。

这应该适用于您给出的示例:

properties.First(p => p.Name == property.Name).Select(o => o.Children().Values<string>()).First().ToArray();

代替:

properties.First(p => p.Name == property.Name).ToObject<string[]>();

关于c# - JSON.Net无法在自定义JsonConverter中反序列化json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47954363/

相关文章:

ruby - 如何按字母顺序对数组进行排序?

python - 如何解析txt中保存的suds对象?

c# - 如何添加到结构体数组中

c# - 获取选择的 Item 的父 ObservableCollection - WP8

java - StringBuffer 的替换方法无法正常工作

php - 如何将文本 append 到特定的 php 数组值

javascript - JSON 语法是 Python 语法的严格子集吗?

java - 使用不同的表达式分割字符串

c# - Unity3d 从定时器访问主线程

c# - 具有继承接口(interface)的抽象类 - System.Enum 实现 IConvertible 但 MethodInfo.GetMethods() 不列出那些