[英]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.