尝试在 PCL 中使用 Refit 反序列化 Json 时出现以下错误:
无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型“System.Collections.Generic.List`1[UserItem]”,因为该类型需要 JSON 数组(例如 [1,2,3] ) 以正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型可以从 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"]
}
}
编辑:这是我实例化 Refit 的方式:
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 更改为动态,然后将动态对象解析为 UserReponse,但它仍然剥夺了兴趣。而且我会失去使用 Refit 的好处:
[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>();
我做错了吗?没有一种简单的方法来检索字符串数组吗?