我知道有很多关于这个的话题,但是我找不到解决方案,也找不出我在这里做错了什么。
场景:
There are 2 application ( both using Newtonsoft.Json & RestSharp)
Application A serializes a list of objects and returns a string
Application B takes takes the string deserializes back to the list
在过去的两天里,我尝试了在互联网上找到的多种解决方案。对我来说什么都不管用..
有人能告诉我这里遗漏了什么吗?
谢谢。
如果我应该添加更多细节,请让我知道。
用于序列化和反序列化的类:
public class Email
{
public string Reference { get; set; }
public string ShortDescription { get; set; }
}
创建响应接口:
//Response
List<Email> apiResponse = new List<Email>();
Task<List<Email>> get = getEmails.GetEmails();
apiResponse = (await get);
return JsonConvert.SerializeObject(apiResponse);
接口响应:
[{"Reference":"01010101","ShortDescription":"Customers Unable To Navigate
Beyond \"Choose A Level\" Screen on appllicaiton"},
{"Reference":"02020202","ShortDescription":"Example2: messenger issue"}]
响应反序列化:
RestClient client = new RestClient("http://servername:81/");
client.ClearHandlers();
RestRequest request = new RestRequest("api/emails/HandleGetRequest", Method.GET);
request.AddHeader("Accept", "application/json");
request.AddQueryParameter("query", queryString);
IRestResponse response = client.Execute(request);
List<Email> apiResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
// OK
apiResponse = JsonConvert.DeserializeObject<List<Email>>(response.Content);
}
else
{
// NOK
apiResponse = null;
Console.Write("ERROR: {0}", response.Content);
log.FatalFormat("ERROR: {0}", response.Content);
}
return apiResponse;
收到错误:
[ArgumentException: Could not cast or convert from System.String to
System.Collections.Generic.List`1[TestProject.Models.Email].]
Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value,
Type initialType, Type targetType) +243
Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue,
CultureInfo culture, Type targetType) +123 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) +485
[JsonSerializationException: Error converting value "
[{"Reference":"01010101","ShortDescription":"Customers Unable To Navigate
Beyond \"Choose A Level\" Screen on appllicaiton"},
{"Reference":"02020202","ShortDescription":"Example2: messenger issue"}]"
to type 'System.Collections.Generic.List`1[TestProject.Models.Email]'. Path '',
line 1, position 6633.]
发布于 2019-02-08 18:12:27
为了详细说明我的评论,我想我已经证明了这个理论。
您可以在this fiddle中看到所描述的JSON的反序列化工作正常。
这里似乎发生的情况是,response.Content
就是那个JSON,但进行了转义。无论您使用的是哪种服务器框架,都可能再次序列化了结果( string
)。
您可以从this fiddle中看到,这会重现相同的错误。
至少替换掉这一行:
return JsonConvert.SerializeObject(apiResponse);
使用简单的
return apiResponse;
应该会导致正确的序列化(如果您需要进一步配置框架而不是查看相关文档,框架将会这样做)。
但是,我也从the docs中注意到,RestSharp内置了自己的JSON反序列化,因此在接收端可能也不需要使用Newtonsoft.Json
。您可以看到,在recommended usages中有各种泛型重载,如Execute<T>
,允许您指定返回类型,框架将对此进行反序列化。
IRestResponse<List<Email>> response = client.Execute<List<Email>>(request);
List<Email> apiResponse = response.Data;
我建议您阅读RestSharp文档和一些示例。
发布于 2019-02-08 17:06:44
我认为问题出在反序列化部分的IList。
尝试:
apiResponse = JsonConvert.DeserializeObject<List<Email>>(response.Content);
https://stackoverflow.com/questions/54588921
复制相似问题