我有一个来自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; }
尝试反序列化时,我收到以下错误消息。
将值“Mail1.Mail@mail.mail”转换为'System.Collections.Generic.IList`1 [System.String]'时出错。路径'[0] .Message-Recipient-Address',第1行,第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;
}
}
任何线索,我做错了,或为什么这不起作用?先感谢您, 斯蒂芬
答案 0 :(得分:0)
通过编写自定义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; }
答案 1 :(得分:-2)
Error converting value "Mail1.Mail@mail.mail" to type
从我所看到的,您正在尝试将单个电子邮件地址反序列化为电子邮件地址列表,这显然会失败。
请发布您用于反序列化输入的任何其他代码。
编辑:我看到你发布了一些代码,但它仍然没有向我们展示反序列化过程。