亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 JSON API 從字典解析為 Xamarin.Forms 中 ListView

將 JSON API 從字典解析為 Xamarin.Forms 中 ListView

C#
慕沐林林 2022-07-23 17:50:53
我正在嘗試解析有錯誤的 API 端點。這是從 API 返回的原始 JSON{"posts":{"29.11.2018":[{"title":"xxx","image_url":null,"message":"xxxx","time":"08:30 AM"}]}這是我的根對象    public class Notification    {        [JsonProperty("posts")]        public Dictionary<string, List<Post>> posts { get; set; }    }    public class Post    {        [JsonProperty("title")]        public string title { get; set; }        [JsonProperty("image_url")]        public Uri imageUrl { get; set; }        [JsonProperty("message")]        public string message { get; set; }        [JsonProperty("time")]        public string time { get; set; }    }我的服務等級var notification = JsonConvert.DeserializeObject<Dictionary<string, Notification>>(apiContent); //Deserialize the JSON datavar _schoolNotification = new ObservableCollection<Post>(notification.Keys);NotificationListView.ItemsSource = _schoolNotification;我編輯的 XAML<StackLayout>    <Label Style="{StaticResource ListViewTitle}" Text="{Binding title}" />   <Label Text="{Binding message}" TextColor="{DynamicResource ListViewDateColor}" /></StackLayout>我面臨的問題是,我無法將此字典解析為 Xamarin.Forms 使用的 ObservableColection。任何幫助將不勝感激。
查看完整描述

2 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

我認為預覽的答案是正確的,但需要鑄造所以試試這個


//Deserialize the JSON data


  var notification = JsonConvert.DeserializeObject<Notification>(apiContent); 


    List<Post> posts = new List<Post>();

      if (notification?.posts != null){

        foreach(var item in notification.posts.Values){

           posts.AddRange(item);

         }

       }

    var _schoolNotification = new ObservableCollection<Post>(posts);


查看完整回答
反對 回復 2022-07-23
?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

我建議通過 jsonutils.com 運行您的 JSON 響應,并將生成的類用作反序列化步驟中的模型。它通常是萬無一失的,并且在嘗試手動定義類時消除了潛在的人為錯誤。



查看完整回答
反對 回復 2022-07-23
  • 2 回答
  • 0 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號