簡體   English   中英

通過api邏輯對對象進行任務和反序列化以及對json反序列化

[英]Task and deserializing objects from api logic and deserializing json

我希望有人可以幫助我逐步理解這些邏輯。 我希望這不是具體,以便可能對其他人有所幫助。 我敢肯定,肯定會是這種方式,但是我不知道這在行業中是否很普遍(我應該是一名經濟學家,但是我會幫助編程,直到我們變得更熟練為止貿易)。

背景:我被要求反序列化由api返回的json對象,但是我不確定我是否正確理解了代碼,因此直到那時我都無法反序列化它。 我的兩個希望是,如果我的邏輯不正確,有人可以糾正我,並幫助我弄清楚如何對其進行反序列化。

目標非常簡單:使用UITableView顯示從api返回的項目的列表。 我已經設置了UITableView。 我現在只需要用數據填充它。 這是我面前寫的。 它是任務/ api:

  public async Task<Food> FoodCatalog(int category)
    {
        string url = Service.baseURL + Service.FoodCatalog + "?category=" + category.ToString();

        dynamic results = await Service.getDataFromService(url).ConfigureAwait(false);
        string json = results as string;     // Otherwise error next line ???

        Food items = JsonConvert.DeserializeObject<Food>(json);

        return items;
    }

這是食品課:

public struct FoodStruct
{
    [JsonProperty(PropertyName = "FoodGroupTypeId")] public short? FoodGroupTypeId;
    [JsonProperty(PropertyName = "FoodGroupDescription")] public string FoodGroupDescription;
    [JsonProperty(PropertyName = "FoodId")] public int? FoodId;
    [JsonProperty(PropertyName = "FoodDescription")] public string FoodDescription;
}

public class Food
{
    [JsonProperty(PropertyName = "Table Selection")] public static List<FoodStruct> FoodList = new List<FoodStruct>();

    public FoodStruct this[int key] { get { return FoodList[key]; } }

    public static string[] ToStringArray()
    {
        string[] list = new string[FoodList.Count];
        int i = 0;

        foreach (FoodStruct fs in FoodList)
        {
            list[i++] = fs.FoodDescription;
        }

        return list;
    }

    public static implicit operator string(Food v)
    {
        throw new NotImplementedException();
    }
}

api的任務可以連接到url,獲取結果,然后將反序列化的對象作為類返回? 這就是這種邏輯的原理嗎?

Food類看起來像我想要的那樣。 看起來它創建了食物描述列表(食物組等還不是很重要)。 但是我無法訪問該食物清單,而且我確實不能完全確定這是否正在按照我的意願進行,但似乎是這樣。

有人可以糾正我的邏輯並幫助我弄清楚如何反序列化對象以將其放入UITableView嗎?

您可以使用Newtonsoft.Json:

string foodJson = "[{\"FoodGroupTypeId\":\"apple\", \"FoodGroupDescription\":\"apple fruit\",\"FoodId\": \"Fuji Apple\",\"FoodDescription\": \"Fuji Apple\"}]";

var obj = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(foodJson);

foreach (var ch in obj.Children())
{
    string id = ch["FoodGroupTypeId"].ToString();  
}

反序列化您的響應,如下所示

public async Task<Food> FoodCatalog(int category)
{
    string url = Service.baseURL + Service.FoodCatalog + "?category=" + category.ToString();
    dynamic results = await Service.getDataFromService(url).ConfigureAwait(false);
    string json = results as string;  // Otherwise error next line ???
    var items = JsonConvert.DeserializeObject<List<Food>>(json);        
    return items;
}

在您的ViewController中綁定您的UITableView

public async override void ViewDidLoad()
{       
    //Create your API class object & call foodCatalog method here
    var items=await FoodCatalog(params)
    //Bind your UITableView
    mainListview.Source = new TableViewSource(items); 
}

有關如何綁定TableView的更多信息,請訪問

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM