无法反序列化当前JSON对象(空数组)

时间:2018-11-09 03:57:15

标签: c# json json.net

我试图使该程序将所有这些对象格式化为树状视图,以实现此目的(我使用JSON排序对象),我需要解析JSON,所以我选择了JSON.NET。

下面是格式设置的一个示例:

{
    "Space": {
        "ClassName": "SpaceObject",
        "Name": "Space",
        "Children": {
            "Object1": {
                "ClassName": "Object",
                "Name": "Object1",
                "Children": []
            },
            "Object2": {
                "ClassName": "Object",
                "Name": "Object2",
                "Children": []
            }
        }
    }
}
public class CObject 
{
    [JsonProperty(PropertyName = "Name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "ClassName")]
    public string ClassName { get; set; }

    [JsonProperty(PropertyName = "Children")]
    public IDictionary<string, CObject> Children { get; set; }
}
IDictionary<string, CObject> obj = JsonConvert.DeserializeObject<IDictionary<string, CObject>>(Json, new JsonSerializerSettings() {
    MissingMemberHandling = MissingMemberHandling.Ignore,
    NullValueHandling = NullValueHandling.Ignore,
});
foreach (var i in obj) {
    ExplorerView1.Nodes.Add(AddObject(i.Value));
}

我相信我发现了错误,这是由于其中没有对象的子数组所致。我不知道该如何解决,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

this answer Deserialize JSON when type can be different

JsonSingleOrEmptyArrayConverter<T>几乎可以满足您的需求。只需对其进行增强,以使当前合同类型既可以是字典合同又可以是对象合同。

首先,如下修改JsonSingleOrEmptyArrayConverter<T>

public class JsonSingleOrEmptyArrayConverter<T> : JsonConverter where T : class
{
    //https://stackoverflow.com/questions/29449641/deserialize-json-when-type-can-be-different?rq=1
    public override bool CanConvert(Type objectType)
    {
        return typeof(T).IsAssignableFrom(objectType);
    }

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

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var contract = serializer.ContractResolver.ResolveContract(objectType);
        // Allow for dictionary contracts as well as objects contracts, since both are represented by 
        // an unordered set of name/value pairs that begins with { (left brace) and ends with } (right brace).
        if (!(contract is Newtonsoft.Json.Serialization.JsonObjectContract 
              || contract is Newtonsoft.Json.Serialization.JsonDictionaryContract))
        {
            throw new JsonSerializationException(string.Format("Unsupported objectType {0} at {1}.", objectType, reader.Path));
        }

        switch (reader.SkipComments().TokenType)
        {
            case JsonToken.StartArray:
                {
                    int count = 0;
                    while (reader.Read())
                    {
                        switch (reader.TokenType)
                        {
                            case JsonToken.Comment:
                                break;
                            case JsonToken.EndArray:
                                // You might want to allocate an empty object here if existingValue is null
                                // If so, do
                                // return existingValue ?? contract.DefaultCreator();
                                return existingValue;
                            default:
                                {
                                    count++;
                                    if (count > 1)
                                        throw new JsonSerializationException(string.Format("Too many objects at path {0}.", reader.Path));
                                    existingValue = existingValue ?? contract.DefaultCreator();
                                    serializer.Populate(reader, existingValue);
                                }
                                break;
                        }
                    }
                    // Should not come here.
                    throw new JsonSerializationException(string.Format("Unclosed array at path {0}.", reader.Path));
                }

            case JsonToken.Null:
                return null;

            case JsonToken.StartObject:
                existingValue = existingValue ?? contract.DefaultCreator();
                serializer.Populate(reader, existingValue);
                return existingValue;

            default:
                throw new InvalidOperationException("Unexpected token type " + reader.TokenType.ToString());
        }
    }

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

public static partial class JsonExtensions
{
    public static JsonReader SkipComments(this JsonReader reader)
    {
        while (reader.TokenType == JsonToken.Comment && reader.Read())
            ;
        return reader;
    }
}

然后反序列化如下:

var settings = new JsonSerializerSettings
{
    MissingMemberHandling = MissingMemberHandling.Ignore,
    NullValueHandling = NullValueHandling.Ignore,               
    Converters = { new JsonSingleOrEmptyArrayConverter<IDictionary<string, CObject>>() },
};

var obj = JsonConvert.DeserializeObject<IDictionary<string, CObject>>(Json, settings);

注意:

  • 我的假设是,仅当没有子级时才使用空数组[]。如果该数组曾经是非空数组,则此假设将是错误的,并且转换器将无法正常工作。

  • 转换器为一个空数组返回一个null值。如果您想改用空字典,请取消注释以下几行:

    // You might want to allocate an empty object here if existingValue is null
    // If so, do
    // return existingValue ?? contract.DefaultCreator();
    

正在工作的.Net小提琴here