简体   繁体   English

通过api逻辑对对象进行任务和反序列化以及对json反序列化

[英]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. 背景:我被要求反序列化由api返回的json对象,但是我不确定我是否正确理解了代码,因此直到那时我都无法反序列化它。 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. 目标非常简单:使用UITableView显示从api返回的项目的列表。 I have the UITableView that is all set up. 我已经设置了UITableView。 I just need to fill it with the data now. 我现在只需要用数据填充它。 Here is what was written before me. 这是我面前写的。 It is the task/api: 它是任务/ 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? api的任务可以连接到url,获取结果,然后将反序列化的对象作为类返回? Is that how this logic works? 这就是这种逻辑的原理吗?

The Food class looks like it does what I want it to. Food类看起来像我想要的那样。 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? 有人可以纠正我的逻辑并帮助我弄清楚如何反序列化对象以将其放入UITableView吗?

You can use Newtonsoft.Json: 您可以使用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 在您的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); 
}

For more information about how to bind TableView visit this 有关如何绑定TableView的更多信息,请访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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