c# - 反序列化呈现为不同类型的节点

标签 c# json json.net

我正在遍历一个 JSON 文件文件夹,并试图从中提取一些信息;但是,我发现这样做非常困难。

我已经开始构建要反序列化的对象。我有一个特定的节点,我通常会反序列化为一个对象,但当它为空时,它会显示为一个空数组。请参阅下面示例 JSON 中的 definition 字段:

{
  "name": "Example",
  "description": "Example JSON",
  "properties": {
    "foo": "bar",
    "foo1": "bar2",
    "foo3": "bar4"
  },
  "stages": {
    "This is a stage": {
      "stageInfo1": "blah",
      "stageInfo2": "blah",
      "integration": {
        "x": "x",
        "y": "y",
        "z": "z",
        "definition": []
      }
    },
    "Another Stage": {
      "stageInfo1": "blah",
      "stageInfo2": "blah",
      "integration": {
        "x": "x",
        "y": "y",
        "z": "z",
        "definition": {
          "5a4d7de4c6518": {
            "Editable": true,
            "ID": "5a4d7de4c6518",
            "Name": "My example"
          }
        }
      }
    }
  }
}

由于定义名称可以更改(在本例中为 5a4d7de4c6518),我认为最好使用字典,但是当出现空数组时会引发错误。

 [JsonProperty("definition")]
 public Dictionary<string, Definition> definition;

错误:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,JsonProcessReader.Models.Stages+IntegrationDefinition]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

最佳答案

空数组与字典结构不兼容,这会导致您看到的错误。由于您似乎无法轻易更改 JSON,因此您需要使用 JsonConverter来处理这种情况。这是一个应该适合您的通用方法:

class TolerantObjectConverter<T> : JsonConverter where T: new()
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(T);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JToken token = JToken.Load(reader);
        object result = new T();
        if (token.Type == JTokenType.Object)
        {
            serializer.Populate(token.CreateReader(), result);
        }
        return result;
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

此转换器的工作原理是将相关的 JSON 片段暂时加载到 JToken 中,然后在尝试转换之前检查它是否真的是一个对象。因此,如果它是一个数组或其他无法正确转换的标记类型,它将返回一个空的 T 实例。

要使用转换器,只需将 [JsonConverter] 属性添加到您的字典属性中,如下所示:

public class Integration
{
    ...
    [JsonConverter(typeof(TolerantObjectConverter<Dictionary<string, Definition>>))]
    public Dictionary<string, Definition> definition { get; set; }
}

这是一个工作演示:https://dotnetfiddle.net/83dQoC

关于c# - 反序列化呈现为不同类型的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52738560/

相关文章:

c# - WCF 服务自主机 url 未获取 wsdl 文档

ios - 如何将json响应数据分解为多个字符串?

java - 无法将字符串转换为长字符串

vb.net - JSON.NET 中的条件对象序列化

json - 使用 Json.NET 将 F# 可变变量序列化为 JSON 会生成重复项

c# - MVC4 - 在每个页面上显示通用弹出消息的最佳方式

c# - 使用 NewtonSoft,如何使用 JArray 创建字符串数组

c# - AutoMapper - 添加自定义格式化程序

ruby-on-rails - 从ruby中的json获取特定的键值

c# - Java 的 "org.json"与 C#'s "Newtonsoft.Json.JsonConvert"