简体   繁体   中英

Task and deserializing objects from api logic and deserializing json

I was hoping someone could help walk me through some of this logic here. I hope this isn't too specific so that maybe it will help other people. It will definitely seem that way, I am sure, but I don't know if this is is common in industry or not (I am supposed to be an economist, but I am helping with the programming until we get someone more skilled at this trade).

The background: I was asked to deserialize a json object that is returned by the api, but I am not sure if I am understanding the code correctly so until then I won't be able to deserialize it. My two hopes are that someone can correct me if my logic is incorrect and to help me figure out how to deserialize it.

The goal is pretty simple: show a list using a UITableView of the items that are returned from the api. I have the UITableView that is all set up. I just need to fill it with the data now. Here is what was written before me. It is the task/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;
    }

Here is the Food class:

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();
    }
}

The api has a task that connects to the url, gets the results, and returns the deserialized object as a class? Is that how this logic works?

The Food class looks like it does what I want it to. It looks like it creates a list of the food description (the food group, etc isn't terribly important yet). But I have no way of accessing that food list and I am really not completely sure this is doing what I want it to, but it seems that way.

Can someone correct me on the logic and help me figure out how to deserialize the object to put it in the UITableView?

You can use 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();  
}

Deserialize you response like below

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;
}

In your ViewController bind your 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); 
}

For more information about how to bind TableView visit this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM