简体   繁体   English

无法将json反序列化为ObservableCollection并绑定到Xamarin.Forms中的xaml

[英]Unable to Deserialize json into an ObservableCollection and bind to xaml in Xamarin.Forms

I am trying to deserialize simple json to an ObservableCollection. 我正在尝试将简单的json反序列化为ObservableCollection。 When I set a breakpoint for the Deserialization nothing seems to happen. 当我为反序列化设置断点时,似乎什么也没有发生。 The method never completes and it doesn't throw an error. 该方法永远不会完成,也不会引发错误。 I am able to get the json and convert to a string, just not deserialize to an ObservableCollection. 我能够获取json并转换为字符串,只是不反序列化为ObservableCollection。

Am I missing something? 我想念什么吗? I have looked at every code example I could find for deserializing to an object and can't seem to make it work. 我看了我能找到的用于反序列化对象的每个代码示例,但似乎无法使其工作。

This is the code for my Xamarin.Forms page. 这是我的Xamarin.Forms页面的代码。

public partial class Notification : ContentPage
{
    public Notification()
    {
        InitializeComponent();
        this.BindingContext = NotificationData();
    }

    public async Task NotificationData()
    {
        ObservableCollection<Alert> notification = await GetAlert();
        NotificationList.ItemsSource = notification;
    }
    public static async Task<ObservableCollection<Alert>> GetAlert()
    {
        string WPPosts = "https://www.url.com/alerts.json";
        HttpClient client = new HttpClient();
        var response = await client.GetAsync(WPPosts).ConfigureAwait(false);

        if (response != null)
        {
            ObservableCollection<Alert> data = new ObservableCollection<Alert>();
            string content = response.Content.ReadAsStringAsync().Result;
            //string content_sanitized = RemoveHTMLTags(content);
            data = JsonConvert.DeserializeObject<ObservableCollection<Alert>>(content);
            return data;
        }
        else { return null; }
    }
    public class Alert
    {
        public string title { get; set; }
        public string imgUrl { get; set; }
        public string content { get; set; }
    }

    public class RootObject
    {
        public List<Alert> alerts { get; set; }
    }
}

This is my Xaml. 这是我的Xaml。

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" Title="News 
and Alerts" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
x:Class="JenksTrash.Notification" xmlns:local="clr-
namespace:JenksTrash" >
<ListView x:Name="NotificationList">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1" />
                        <ColumnDefinition Width="7*" />
                    </Grid.ColumnDefinitions>
                        <Label Text="{Binding title}" FontAttributes="Bold" />
                        <Label Text="{Binding content}" FontAttributes="Bold" />
                    </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Any help understanding this would be amazing. 任何帮助理解这一点将是惊人的。

alerts are nested under a root object, hence why you can't deserialize it directly. alerts嵌套在根对象下,因此为什么不能直接对其进行反序列化。

But, you can try this: 但是,您可以尝试以下操作:

JObject.Parse(content)
    .SelectToken("alerts")
    .ToObject<ObservableCollection<Alert>>();

This should help you understand the problem: 这应该可以帮助您了解问题: 在此处输入图片说明

The json that you have is like RootObjectWithNestedNumberList , but you were trying to deserialize it as a SimpleNumberList . 您拥有的json类似于RootObjectWithNestedNumberList ,但是您试图将其反序列化为SimpleNumberList Obviously, it wasn't going to work. 显然,这是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin.Forms 无法通过 ViewModel 将 ObservableCollection 绑定到 CollectionList - Xamarin.Forms unable to bind ObservableCollection to CollectionList via ViewModel 将字典中的JSON API解析为Xamarin.Forms中的ListView的ObservableCollection - Parse JSON API from Dictionary to ObservableCollection for ListView in Xamarin.Forms Xamarin.Forms:绑定到 XAML 中的代码隐藏属性 - Xamarin.Forms: bind to a code behind property in XAML 将 XAML “Label”绑定到 Xamarin.Forms 中“public const”后面的代码? - Bind XAML “Label” to Code Behind “public const” in Xamarin.Forms? 在Xamarin.forms中无法在XAML页面上看到绑定 - Unable to see Bindings on XAML page in Xamarin.forms Json 反序列化表示儿童无法评估 - xamarin.forms - Json deserialize says children could not evaluate- xamarin.forms json.deserialize返回null + xamarin.forms - json.deserialize returns null + xamarin.forms 如何使用 xamarin.forms 将两个 xaml 和 xaml.cs 文件绑定到一个 ViewModel? - How to bind two xaml and xaml.cs files to one ViewModel using xamarin.forms? 反序列化 Json, Xamarin Forms - Deserialize Json, Xamarin Forms 有没有办法将元素的 xaml 属性绑定到由代码创建的新元素? (c# xamarin.forms) - Is there a way to bind the xaml properties of an element to a new element created by code? (c# xamarin.forms)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM