RestSharp反序列化列表返回无法施放或从System.String转换为

问题描述 投票:2回答:2

我知道,有很多话题有关这一点,悬停,我无法找到一个解决方案或找出我在做什么错在这里。

场景:

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 

对于最后2天我试过多种解决方案,我发现在互联网上..没有工作对我来说..

可有人请指教一下我在这里失踪?

谢谢。

请让我知道我是否应该增加更多的细节。

用于序列化和反序列化类:

public class Email
{
    public string Reference { get; set; }
    public string ShortDescription { get; set; }
}

API来创建响应:

//Response
List<Email> apiResponse = new List<Email>();
Task<List<Email>> get = getEmails.GetEmails();
apiResponse = (await get);
return JsonConvert.SerializeObject(apiResponse);

API响应:

[{"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.]
c# json c#-4.0 deserialization restsharp
2个回答
2
投票

为了详细说明注释现在我想我已经证明了理论...

您可以在this fiddle,作为描述的JSON反序列化工作正常看到的。

什么似乎是这里发生的是,response.Content是JSON,但逃脱。这可能是因为您使用再次序列化的结果(string)的任何服务器框架。

您可以从this fiddle看到,这再现了同样的错误。

至少,更换线路:

return JsonConvert.SerializeObject(apiResponse);

与简单

return apiResponse;

应导致正确的序列化(框架将做到这一点,如果你需要配置它进一步比看一下相关的文档)。

不过,我也想从the docs是RestSharp内置了自己的JSON deserialisation注意,所以可能没有必要要么在接收端使用Newtonsoft.Json。您可以在recommended usages看到有喜欢Execute<T>各种通用重载允许您指定的返回类型和框架将deserialise这一点。

IRestResponse<List<Email>> response = client.Execute<List<Email>>(request);
List<Email> apiResponse = response.Data;

我建议你阅读RestSharp文档和一些例子。


1
投票

我认为这个问题是在反序列化部分IList中。

尝试:

apiResponse = JsonConvert.DeserializeObject<List<Email>>(response.Content);
© www.soinside.com 2019 - 2024. All rights reserved.