我正在尝试将简单的json反序列化为ObservableCollection。当我为反序列化设置断点时似乎没有发生任何事情。该方法永远不会完成,也不会抛出错误。我能够获取json并转换为字符串,而不是反序列化为ObservableCollection。
我错过了什么吗?我已经查看了我可以找到的用于反序列化到对象的每个代码示例,但似乎无法使其工作。
这是我的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; }
}
}
这是我的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>
任何理解这一点的帮助都会很棒。
答案 0 :(得分:0)
Sub Vgbl()
Dim IE As Object
Dim n, Period1, Period2 As Double
Dim MAPFREVI, VIDASEG, CS, Alianca, Brasilv, MapfreSeg, BrasilPrev, SEGUROS, TODAS, BrasilCap As Variant
SEGUROS = "54"
TODAS = "Todas"
Period1 = Sheets("Capa").Cells(4, 3).Value
Period2 = Sheets("Capa").Cells(5, 3).Value
n = Sheets("Ramos").Cells(65, 4).Value
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://www2.susep.gov.br/menuestatistica/SES/principal.aspx"
IE.Visible = True
While IE.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("00:00:02"))
'seleciona as operações desejadas
IE.document.getElementById("ctl00_ContentPlaceHolder1_edSelOp").Value = SEGUROS
IE.document.getElementById("ctl00_ContentPlaceHolder1_btnConsultar").Click
'seleciona o periodo
Application.Wait (Now + TimeValue("00:00:02"))
Set ieDoc = IE.document
ieDoc.getElementById("ctl00_ContentPlaceHolder1_edInicioPer").Value = Period1
ieDoc.getElementById("ctl00_ContentPlaceHolder1_edFimPer").Value = Period2
'seleciona as empresas
IE.document.getElementById("ctl00_ContentPlaceHolder1_edEmpresas").SelectedIndex = TODAS
'seleciona os ramos
Set objListbox = IE.document.getElementById("ctl00_ContentPlaceHolder1_edRamos")
objListbox.Options(n).Selected = True
n = Sheets("Ramos").Cells(66, 4).Value
objListbox.Options(n).Selected = True
n = Sheets("Ramos").Cells(67, 4).Value
objListbox.Options(n).Selected = True
End sub
嵌套在根对象下,因此您无法直接对其进行反序列化。
但是,你可以试试这个:
alerts
您拥有的json与JObject.Parse(content)
.SelectToken("alerts")
.ToObject<ObservableCollection<Alert>>();
类似,但您尝试将其反序列化为RootObjectWithNestedNumberList
。显然,它不会起作用。