RestSharp 反序列化列表 <object> returns 无法从 System.String 转换或转换为
RestSharp Deserialize List<object> returns Could not cast or convert from System.String to
我知道有很多关于这个的话题,悬停,我在这里找不到解决方案或弄清楚我做错了什么。
场景:
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 天里,我尝试了在互联网上找到的多种解决方案.. 对我来说没有任何效果..
有人可以告诉我这里缺少什么吗?
谢谢。
如果我需要添加更多详细信息,请告诉我。
Class 用于序列化和反序列化:
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.]
我认为问题出在反序列化部分的 IList。
尝试:
apiResponse = JsonConvert.DeserializeObject<List<Email>>(response.Content);
现在详细说明评论我想我已经证明了这个理论...
您可以在 this fiddle 中看到 JSON 所描述的反序列化工作正常。
这里似乎发生的是 response.Content
是 JSON,但逃脱了。您使用的任何服务器框架都可能再次序列化了结果(string
)。
您可以从 this fiddle 中看出,这会重现相同的错误。
至少更换行:
return JsonConvert.SerializeObject(apiResponse);
简单地
return apiResponse;
应该导致正确的序列化(如果您需要进一步配置而不是查看相关文档,框架将执行此操作)。
但是,我还要从 the docs that RestSharp has its own JSON deserialisation built in, so there's probably no need to use Newtonsoft.Json
on the receiving end either. You can see in the recommended usages 中注意到有各种通用重载,例如 Execute<T>
允许您指定 return 类型,并且框架将对此进行反序列化。
IRestResponse<List<Email>> response = client.Execute<List<Email>>(request);
List<Email> apiResponse = response.Data;
我建议阅读 RestSharp 文档和一些示例。
我知道有很多关于这个的话题,悬停,我在这里找不到解决方案或弄清楚我做错了什么。
场景:
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 天里,我尝试了在互联网上找到的多种解决方案.. 对我来说没有任何效果..
有人可以告诉我这里缺少什么吗?
谢谢。
如果我需要添加更多详细信息,请告诉我。
Class 用于序列化和反序列化:
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.]
我认为问题出在反序列化部分的 IList。
尝试:
apiResponse = JsonConvert.DeserializeObject<List<Email>>(response.Content);
现在详细说明评论我想我已经证明了这个理论...
您可以在 this fiddle 中看到 JSON 所描述的反序列化工作正常。
这里似乎发生的是 response.Content
是 JSON,但逃脱了。您使用的任何服务器框架都可能再次序列化了结果(string
)。
您可以从 this fiddle 中看出,这会重现相同的错误。
至少更换行:
return JsonConvert.SerializeObject(apiResponse);
简单地
return apiResponse;
应该导致正确的序列化(如果您需要进一步配置而不是查看相关文档,框架将执行此操作)。
但是,我还要从 the docs that RestSharp has its own JSON deserialisation built in, so there's probably no need to use Newtonsoft.Json
on the receiving end either. You can see in the recommended usages 中注意到有各种通用重载,例如 Execute<T>
允许您指定 return 类型,并且框架将对此进行反序列化。
IRestResponse<List<Email>> response = client.Execute<List<Email>>(request);
List<Email> apiResponse = response.Data;
我建议阅读 RestSharp 文档和一些示例。