我在PCL中尝试通过重新装配取消Json的序列化时出现以下错误:
无法反序列化当前JSON对象(例如{"name":"value"}(转换为类型"System.Collections.Generic.List `1[UserItem]",因为类型需要JSON数组(例如[1,2,3](才能正确反序列化。到修复此错误,或者将JSON更改为JSON数组(例如[1,2,3](或者更改反序列化的类型,使其为正常的.NET类型(例如。不是像integer那样的基元类型,也不是像数组那样的集合类型或List(,其可以从JSON对象反序列化。JsonObjectAttribute也可以添加到类型中,以强制它从JSON对象反序列化。路径"user.id",第1行,位置42。
我认为它来自为兴趣返回的数组?
{
"error": {
"code": 0,
"msg": ""
},
"user": {
"id": "5",
"first_name": "test",
"last_name": "test",
"email": "a@gmail.com",
"auth_token": "****",
"date_of_birth": "0001-05-06 00:00:00",
"group_id": "1",
"group_name": null,
"postal_code": "56456",
"city": "Annecy",
"country": "France",
"facebook_id": null,
"facebook_img": null,
"status": null,
"is_activated": null,
"gcm_id": "GHJK",
"device_id": null,
"platform": "android",
"interest": ["1", "2"],
"profile_pic": "profile.jpg",
"cover_img": "cover.jpg",
"address": null,
"invoicing_address": "address",
"invoicing_city": "city",
"invoicing_country": "",
"invoicing_postal_code": "78654",
"gender": null,
"company_no": "1234",
"company_name": "",
"about_company": "",
"company_logo": "",
"company_legal_name": "lumao",
"company_contact_no": "",
"company_address": "",
"company_city": "",
"company_country": "",
"company_postal_code": "",
"vat_no": "",
"telephone": null,
"membership_status": null,
"contact_status": 2,
"company_interests": [],
"needs": ["not_implemented"]
}
}
编辑:以下是我如何实例化重新装配:
Func<HttpMessageHandler, IFlairPlayApi> createClient = messageHandler =>
{
var client = new HttpClient(messageHandler)
{
BaseAddress = new Uri(ApiBaseAddress)
};
return RestService.For<IFlairPlayApi>(client);
};
NativeMessageHandler msgHandler = new NativeMessageHandler();
msgHandler.DisableCaching = true;
_speculative = new Lazy<IFlairPlayApi>(() => createClient(
new RateLimitedHttpMessageHandler(msgHandler, Priority.Speculative)
));
以及我如何呼叫服务:
[Get("/getuser.json")]
Task<UserResponse> GetUser(int userid, int contact_id, string langval);
编辑2:我试图将UserResponse更改为dynamic,然后将动态对象解析为UserRepense,但它仍然会剥离兴趣。我会失去使用重新装配的好处:
[Get("/getuser.json")]
Task<dynamic> GetUser(int userid, int contact_id, string langval);
dynamic userObject = await FPApi.Speculative.GetUser(user.id, contact_id, FPEngine.Instance.Lang);
JObject jUser = userObject as JObject;
UserResponse response = jUser.ToObject<UserResponse>();
我做错了吗?难道没有一种简单的方法来检索字符串数组吗?
我遇到了同样的问题,我按如下方式解决了它:
服务返回,返回字符串,即原始Json:
public interface IApiHgFinance
{
[Get("/taxes?key=minhachave")]
Task<string> GetTaxes();
}
在我调用服务的代码中,我处理Json:
try
{
IApiHgFinance ApiHgFinance = Refit.RestService.For<IApiHgFinance>("https://api.hgbrasil.com/finance");
var result = await ApiHgFinance.GetTaxes();
var jo = Newtonsoft.Json.Linq.JObject.Parse(result);
var taxes = jo["results"].ToObject<itemTaxes[]>();
foreach (var item in taxes)
{
MessageBox.Show($"Taxa CDI : {item.Cdi} nTaxa Selic : {item.Selic} nData Atualização: {item.Date}",
"Atualização de informações", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show($"Erro: {ex.Message}", "Erro ao tentar recuperar as taxas do dia.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}