【问题标题】:Task and deserializing objects from api logic and deserializing json任务和从 api 逻辑反序列化对象和反序列化 json
【发布时间】: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


    【解决方案1】:

    反序列化您的响应,如下所示

    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

    【讨论】:

    • 这太棒了。等我回家后才能尝试,但它很容易理解,并不难。谢谢!
    • 很高兴听到它对您有所帮助。
    • 您介意我再寻求帮助吗?我知道您向我发送了有关如何绑定它的链接,但我仍然遇到问题。我之前制作了一个表格视图,它本质上是一样的,但它肯定给我带来了问题。
    • 其实我想通了!不过非常感谢您的指导。没有它就做不到!
    【解决方案2】:

    您可以使用 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();  
    }
    

    【讨论】:

    • 谢谢!你能告诉我这里的逻辑吗?我会把这个放在哪里? api 是类中的一个方法,因此在它所在的方法之外无法获取 json 变量。
    • 添加了一个示例 json。让我知道是否有帮助
    猜你喜欢
    • 2020-10-09
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 2011-08-14
    • 2022-01-20
    • 2020-10-23
    相关资源
    最近更新 更多