0

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.

I have json that looks like this, it represents a list of cabinet object:

{"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:

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 json from the website and stores it in a listCabinet variable.

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

How can I solve this?

4
  • Unrelated, but why not await the ReadAsStringAsync too? Commented May 8, 2017 at 12:21
  • 2
    You aren't returning anything, you should show that code. You also use .Result which is bad practice, use await instead like before. Commented May 8, 2017 at 12:21
  • listeCabinets is of type CabinetList, and your return value should be of type List<CabinetsList>. You'll need to convert CabinetsList to List<Cabinet>. Commented May 8, 2017 at 12:22
  • May be you need to declare method as public async Task<CabinetsList> loadCabinets() Commented May 8, 2017 at 12:25

1 Answer 1

2

The return type of your method is List<CabinetsList>, while the output of DeserializeObject<CabinetsList> is a CabinetsList. You should deserialize the JSON as the type you want to return:

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:

return new List<CabinetsList> { JsonConvert.DeserializeObject<CabinetsList>(json) };

Side note: and of course await the result:

var json = await response.Content.ReadAsStringAsync();
Sign up to request clarification or add additional context in comments.

3 Comments

Whilst this is most likely correct, I believe it would be worth noting that the OP uses .Result on the ReadAsStringAsync method when they should be using await.
Does he really do that? Edited @TheLethalCoder
Thank you, I'll try and let you know. Sorry if there is any mistakes in the code I'm still learning how to use asynchronous coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.