【问题标题】:JSON.Net cannot deserialize json array in custom JsonConverterJSON.Net 无法反序列化自定义 JsonConverter 中的 json 数组
【发布时间】:2018-06-05 20:47:39
【问题描述】:

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

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

错误的确切文本是:Unexpected token while deserializing object: PropertyName. Path 'RootPages', line 1, position 13.

从检查器中可以看出,它尝试反序列化的 JProperty (RootPages) 已被正确解析并且是有效的 JSON。

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

如果相关,原始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;

    ...
}

【问题讨论】:

  • 当然,我认为这可能是一个问题,但我想我会先移植它看看!

标签: c# arrays json serialization json.net


【解决方案1】:

我希望这是因为第一个属性是一个对象,其属性名为“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[]>();

【讨论】:

  • 谢谢!看起来这成功了。您的回答确实有效,但是在阅读了您的解释后,我也注意到我必须在 .ToObject 之前添加一个 .Value 并且图书馆能够为我计算出该转换
猜你喜欢
  • 2016-03-28
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多