繁体   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