Reputation: 391
I want to show data in my Windows Phone 8.1 from my webpage http://www.veligovsek.si/events/apis/events.php
So I paste this JSON as Classes into FSfeed.cs
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public string name { get; set; }
public string picture_url { get; set; }
public string city { get; set; }
public string date { get; set; }
}
Then I call the API and store the response in a variable: (BasipPage1.xaml.cs)
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedTo(e);
HttpClient http = new HttpClient();
var response = await http.GetStringAsync("http://www.veligovsek.si/events/apis/events.php");
var FSfeed = JsonConvert.DeserializeObject<Rootobject>(response);
Reviews.ItemsSource = FSfeed.Property1;
}
At the end, I want to show this data. So I create a list view in BasicPage1.xaml and define data template for it.
<Page
x:Class="EventHub.BasicPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:EventHub"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Page.Background>
<Grid x:Name="LayoutRoot" Background="#FFFF8A00">
<Grid.ChildrenTransitions>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Grid.ChildrenTransitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Title Panel -->
<StackPanel Grid.Row="0" Margin="19,0,0,0">
<StackPanel.Background>
<ImageBrush Stretch="Fill"/>
</StackPanel.Background>
</StackPanel>
<!--TODO: Content should be placed within the following grid-->
<Grid x:Name="ContentRoot" Margin="0,129,0,0" Grid.RowSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="199*"/>
<ColumnDefinition Width="201*"/>
</Grid.ColumnDefinitions>
<ListView x:Name="Reviews" Grid.ColumnSpan="2" HorizontalAlignment="Right" Width="400" Margin="0,10,0,0">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110">
<Image Source="http://www.nasa.gov/images/content/64883main_image_feature_211_jw4.jpg" Stretch="UniformToFill" />
</Border>
<StackPanel Grid.Column="1" VerticalAlignment="Top" Orientation="Vertical" Margin="10,0,0,0">
<TextBlock Text="{Binding Class1.name}"/>
<TextBlock Text="{Binding Class1.city}" MaxHeight="60"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
<Grid x:Name="asd" HorizontalAlignment="Left" Height="129" Grid.RowSpan="2" VerticalAlignment="Top" Width="400">
<AppBarButton RenderTransformOrigin="0.5,0.5" Height="55" Width="67" Margin="153,33,0,0" Icon="Home">
<AppBarButton.RenderTransform>
<CompositeTransform ScaleX="2" ScaleY="2"/>
</AppBarButton.RenderTransform>
</AppBarButton>
</Grid>
</Grid>
<Page.BottomAppBar>
<CommandBar IsSticky="True" x:Name="appBar">
<CommandBar.PrimaryCommands>
<AppBarButton Icon="Refresh" Label="refresh"/>
<AppBarButton x:Name="capture" Icon="Scan" Label="capture" Click="capture_Click"/>
</CommandBar.PrimaryCommands>
<CommandBar.SecondaryCommands>
<AppBarButton Label="settings"/>
<AppBarButton x:Name="PinUnPinCommandButton" Label="pin to start" Click="PinUnPinCommandButton_Click"/>
<AppBarButton x:Name="about" Label="about" Click="about_click"/>
<AppBarButton x:Name="logout" Label="log out" Click="logout_Click"/>
</CommandBar.SecondaryCommands>
</CommandBar>
</Page.BottomAppBar>
</Page>
But when I run this code to my phone, the following Error shows up...
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
Anyone know what did I do wrong?
Upvotes: 2
Views: 91
Reputation: 15354
Your json is an array/list (See [{...},{...}]
). You should deserialize to List<Class1>
var FSfeed = JsonConvert.DeserializeObject<List<Class1>>(response);
Reviews.ItemsSource = FSfeed;
Upvotes: 1