string uri = Application.Current.Properties["Uri"].ToString();
var client = new HttpClient();
var result = await client.GetAsync(uri "/products");
var resultparsing = JsonConvert.DeserializeObject<ProductResponse>(await
result.Content.ReadAsStringAsync());
var list = JsonConvert.DeserializeObject<List<Item>>(resultparsing.products.ToString());
當結果決議僅傳遞物件名稱時,代碼正在運行,反序列化問題來自 listtostring,在這種情況下,它只是說它是一個串列,而不是在嘗試獲取專案時將串列本身傳遞給 var list。
public class Item
{
public string _id { get; set; }
public string name { get; set; }
public string location { get; set; }
}
public class ProductResponse
{
public string count { get; set; }
public List<Item> products { get; set; }
}
最初被捕獲的 Json 示例
{"count":2,"products":[{"name":"item","location":"storeroom","_id":"6219602068c9a900043fe844"},{"name":"item2","location":"storeroom","_id":"6219603768c9a900043fe850"}]}
uj5u.com熱心網友回復:
你只需要反序列化一次
var json = await result.Content.ReadAsStringAsync();
var resp = JsonConvert.DeserializeObject<ProductResponse>(json);
resp是一個ProductResponse物件,應該包含products屬性中的所有產品詳細資訊。您不需要單獨反序列化產品
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/437363.html
