JSON.NET反序列化转换为 IList 时出错<string>

JSON.NET Deserialize Error converting to IList<string>

我有一份来自 Tika Rest Service 的 json 邮件收件人列表。该列表如下所示:

"Message-Recipient-Address": [
  "Mail1.Mail@mail.mail",
  "Mail2.Mail@mail.mail"]

在我的模型中我有

[JsonProperty("Message-Recipient-Address")]
public IList<string> MailRecipientAddress { get; set; }

尝试反序列化时出现以下错误消息。

Error converting value "Mail1.Mail@mail.mail" to type 'System.Collections.Generic.IList`1[System.String]'. Path '[0].Message-Recipient-Address', line 1, position 319

这是我的解串器 (Restsharp) 的代码

public class TikaDeserializer : IDeserializer
{
    private Newtonsoft.Json.JsonSerializer serializer;

    public TikaDeserializer(Newtonsoft.Json.JsonSerializer serializer)
    {
        this.serializer = serializer;
    }

    public T Deserialize<T>(RestSharp.IRestResponse response)
    {
        var content = response.Content;

        using(var stringReader = new StringReader(content))
        {
            using(var jsonTextReader = new JsonTextReader(stringReader))
            {

                return serializer.Deserialize<T>(jsonTextReader);
            }
        }
    }

    public string DateFormat { get; set; }

    public string Namespace { get; set; }

    public string RootElement { get; set; }

    public static TikaDeserializer Default
    {
        get
        {
            return new TikaDeserializer(new Newtonsoft.Json.JsonSerializer()
            {
                NullValueHandling = NullValueHandling.Ignore,
            });
        }
    }
}

这里是主程序代码

class Program
{
    static void Main(string[] args)
    {
        var request = new RestRequest("rmeta", Method.PUT);
        request.AddHeader("Accept", "application/json");
        request.AddParameter("application/octet-stream", File.ReadAllBytes(@"D:\Wienchol.msg"),ParameterType.RequestBody);
        var response = Execute<List<ParsedDocument>>(request);

    }



    public static T Execute<T>(RestRequest request) where T : new()
    {
        var client = Client(@"http://localhost:9998");
        client.AddHandler("application/json", TikaDeserializer.Default);
        client.AddHandler("text/json", TikaDeserializer.Default);
        client.AddHandler("text/x-json", TikaDeserializer.Default);
        client.AddHandler("text/javascript", TikaDeserializer.Default);
        client.AddHandler("*+json", TikaDeserializer.Default);

        var response = client.Execute<T>(request);

        if(response.ErrorException != null)
        {
            throw new SystemException(response.ErrorMessage);
        }

        return response.Data;
    }
}

我做错了什么或为什么这不起作用的任何线索?先感谢您, 斯特凡

Error converting value "Mail1.Mail@mail.mail" to type 

据我所知,您正试图将单个电子邮件地址反序列化为电子邮件地址列表,这显然失败了。

请 post 您用于反序列化输入的任何其他代码。

编辑:我看到你已经 post 编辑了一些代码,但它仍然没有向我们展示反序列化过程。

通过编写自定义 Json 转换器找到了解决我的问题的方法:

public class JsonStringListConverter: JsonConverter
{

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {

        if(reader.TokenType == JsonToken.Null)
        {
            return null;
        }

        List<string> items = new List<string>();

        if(reader.TokenType == JsonToken.String)
        {

            string value = reader.Value.ToString();
            items.Add(value);
        }

        if(reader.TokenType == JsonToken.StartArray)
        {

            reader.Read();
            do
            {
                string value = reader.Value.ToString();
                items.Add(value);
                reader.Read();
            } while (reader.TokenType != JsonToken.EndArray);
        }

        return (items.Count = 0) ? null : items;
    }



    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(IList<string>));
    }

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

在我的模型中

[JsonProperty("Message-Recipient-Address")]
[JsonConverter(typeof(JsonStringListConverter))]
public IList<string> MailRecipientAddress { get; set; }