从api逻辑任务和反序列化对象并反序列化json

问题描述 投票:0回答:2

我希望有人可以帮助我完成一些逻辑。我希望这不是太具体,所以它可能会帮助其他人。我肯定这看起来肯定会这样,但我不知道这是否在工业界是否常见(我应该是一名经济学家,但我正在帮助编程,直到我们让一个人更熟练贸易)。

背景:我被要求反序列化由api返回的json对象,但我不确定我是否正确理解代码,所以在那之前我将无法对其进行反序列化。我的两个希望是,如果我的逻辑不正确,有人可以纠正我,并帮助我弄清楚如何反序列化它。

目标非常简单:使用从api返回的项目的UITableView显示列表。我有所有设置的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;
    }

这是Food类:

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?

c# json xamarin xamarin.ios
2个回答
0
投票

反汇编你的反应如下

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的更多信息,请访问this


0
投票

你可以使用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();  
}
© www.soinside.com 2019 - 2024. All rights reserved.