[英]Can't return my result in async method
In an MVVM pattern I'm trying to make an async
method that fetches a json
string, and returns the result as a List<Cabinet>
so that I can use this list later. 在MVVM模式中,我试图制作一个
async
方法来获取json
字符串,并将结果作为List<Cabinet>
返回,以便以后可以使用此列表。
I have json
that looks like this, it represents a list of cabinet object: 我有一个看起来像这样的
json
,它代表一个橱柜对象列表:
{"cabinets":[{"id":"1","longitudeGPS":"2,2891506","latitudeGPS":"48,8618687","cp":"75016","ville":"Paris","rue":"1 Avenue Gustave V de Su\u00e8de"},{"id":"2","longitudeGPS":"3,0566481","latitudeGPS":"50,6302592","cp":"59800","ville":"Lille","rue":"127 Rue Solf\u00e9rino"},{"id":"3","longitudeGPS":"3,8749271","latitudeGPS":"43,6110374","cp":"34000","ville":"Montpellier","rue":"17 Rue Foch"},{"id":"4","longitudeGPS":"-1,6736931","latitudeGPS":"48,1181043","cp":"35700","ville":"Rennes","rue":"127 23 Rue de Vincennes"},{"id":"5","longitudeGPS":"0,0991065","latitudeGPS":"49,4931952","cp":"76600","ville":"Le Havre","rue":"32 Avenue Foch"},{"id":"6","longitudeGPS":"4,8353320","latitudeGPS":"45,7639021","cp":"69002","ville":"Lyon","rue":"27 Rue Henri Germain"}]}
I've created the classes according to json2csharp generator: 我已经根据json2csharp生成器创建了类:
public class Cabinet
{
public string id { get; set; }
public string longitudeGPS { get; set; }
public string latitudeGPS { get; set; }
public string cp { get; set; }
public string ville { get; set; }
public string rue { get; set; }
}
public class CabinetList
{
public List<Cabinet> cabinets { get; set; }
}
Here I have an HttpClient
that fetches the jso
n from the website and stores it in a listCabinet
variable. 在这里,我有一个
HttpClient
,它从网站获取jso
n并将其存储在listCabinet
变量中。
public string adresseCabinets = "http://ppe3JoJuAd/gsbAppliFraisV2/webservices/w_cabinet.php";
public static string userId;
CabinetsList listeCabinets = new CabinetsList();
public async Task<List<CabinetsList>>loadCabinets()
{
HttpClient clientCabinets = new HttpClient();
var response = await clientCabinets.GetAsync(adresseCabinets);
var json = response.Content.ReadAsStringAsync().Result;
listeCabinets = JsonConvert.DeserializeObject<CabinetsList>(json);
}
When I want to return the listeCabinets
variable I have the following error: cannot implicitly convert type cabinetList to System.Collection.Generic.List CabinetList
当我想返回
listeCabinets
变量时,出现以下错误: cannot implicitly convert type cabinetList to System.Collection.Generic.List CabinetList
How can I solve this? 我该如何解决?
The return type of your method is List<CabinetsList>
, while the output of DeserializeObject<CabinetsList>
is a CabinetsList
. 方法的返回类型为
List<CabinetsList>
,而DeserializeObject<CabinetsList>
的输出为CabinetsList
。 You should deserialize the JSON as the type you want to return: 您应该反序列化JSON作为要返回的类型:
return JsonConvert.DeserializeObject<List<CabinetsList>>(json);
Or create a new list to add the one and only cabinet to if the JSON contains just one: 或者,如果JSON仅包含一个,则创建一个新列表以添加一个和唯一的文件柜:
return new List<CabinetsList> { JsonConvert.DeserializeObject<CabinetsList>(json) };
Side note: and of course await
the result: 附注:当然
await
的结果:
var json = await response.Content.ReadAsStringAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.