Car*_*s28 2 c# json servicestack .net-core
我已经将Web服务迁移到.NET Core 2.0,它可以正常工作,但是在以json字符串获取响应时遇到问题。
api上的方法:
[HttpGet]
[ProducesResponseType(typeof(string), 200)]
[ProducesResponseType(typeof(EntityNotFoundErrorResult), 404)]
public async Task<IActionResult> GetCurrenciesAsync()
{
var result = await service.GetByTypeAsync(ObjectType.Currency.ToString());
return Ok(result);
}
Run Code Online (Sandbox Code Playgroud)
返回正确的json为result:
"{\"elements\": [{\"Id\": \"1\", \"Symbol\": \"PLN\"},{\"Id\": \"2\", \"Symbol\": \"SIT\"}]}"
Run Code Online (Sandbox Code Playgroud)
然后在客户端上,我正在使用服务堆栈来获取我的货币作为字符串:
public virtual IEnumerable<CurrencyData> GetCurrencies()
{
string response = null;
try
{
response = api.Get<string>("/Currencies");
}
catch (Exception e)
{
}
return string.IsNullOrEmpty(response) ? null : JsonConvert.DeserializeObject<TempCurrencyClass>(response).elements;
}
Run Code Online (Sandbox Code Playgroud)
而response看起来像这样:
"\"{\\\"elements\\\": [{\\\"Id\\\": \\\"1\\\", \\\"Symbol\\\": \\\"PLN\\\"},{\\\"Id\\\": \\\"2\\\", \\\"Symbol\\\": \\\"SIT\\\"}]}\""
Run Code Online (Sandbox Code Playgroud)
因此JsonConvert.DeserializeObject抛出反序列化异常:
Newtonsoft.Json.JsonSerializationException
Run Code Online (Sandbox Code Playgroud)
但是当我反序列化对刺痛和目标的回应时,它起作用:
var x = JsonConvert.DeserializeObject<string>(response);
var deserialized = JsonConvert.DeserializeObject<TempCurrencyClass>(x).elements;
Run Code Online (Sandbox Code Playgroud)
我猜问题出在客户身上吗?奇怪的是,它在.NET Core 1.1上可以正常工作
我缺少什么?
那么它出现在.NET 2.0的核心[Produces("application/json")]发生了变化,它是序列化字符串输出到JSON所以我是有两次连载...这个操作系统的解决方案,以取代[Produces("application/json")]与 [Produces("text/plain")]额在方法/控制器
| 归档时间: |
|
| 查看次数: |
409 次 |
| 最近记录: |