【发布时间】:2018-07-27 01:56:06
【问题描述】:
我希望有人可以帮助我了解这里的一些逻辑。我希望这不是太具体的,这样也许它会帮助其他人。我敢肯定,看起来肯定是这样,但我不知道这在行业中是否常见(我应该是一名经济学家,但我正在帮助编程,直到我们找到更熟练的人贸易)。
背景:我被要求反序列化 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