1
我已经将我的Web服务迁移到.NET Core 2.0,它工作正常,但我有作为JSON字符串获取响应的问题。在API迁移到.NET核心2.0的Web服务和返回json
方法:
[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);
}
即返回正确的JSON作为result
:
"{\"elements\": [{\"Id\": \"1\", \"Symbol\": \"PLN\"},{\"Id\": \"2\", \"Symbol\": \"SIT\"}]}"
然后在客户端我使用service stack让我的货币为字符串:
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;
}
而response
看起来像这样:
"\"{\\\"elements\\\": [{\\\"Id\\\": \\\"1\\\", \\\"Symbol\\\": \\\"PLN\\\"},{\\\"Id\\\": \\\"2\\\", \\\"Symbol\\\": \\\"SIT\\\"}]}\""
所以JsonConvert.DeserializeObject
引发反序列化异常:
Newtonsoft.Json.JsonSerializationException
但是当我反序列化响应刺痛和objest然后它的工作原理:
var x = JsonConvert.DeserializeObject<string>(response);
var deserialized = JsonConvert.DeserializeObject<TempCurrencyClass>(x).elements;
我猜测,这个问题在客户端上吗?奇怪的是,它在.NET核心1.1上工作得很好。
我失踪了什么?