gpt4 book ai didi

c# - Windows 手机 REST API

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:18 30 4
gpt4 key购买 nike

我想在我的 Windows Phone 8.1 中显示网页中的数据 http://www.veligovsek.si/events/apis/events.php

所以我将这个 JSON 作为类粘贴到 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; }
}

然后我调用 API 并将响应存储在一个变量中:(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;
}

最后,我想展示这个数据。因此,我在 BasicPage1.xaml 中创建了一个 ListView 并为其定义了数据模板。

<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>

但是当我在手机上运行这段代码时,会出现以下错误...

            if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();

谁知道我做错了什么?

最佳答案

您的 json 是一个数组/列表(参见 [{...},{...}] )。您应该反序列化为 List<Class1>

var FSfeed = JsonConvert.DeserializeObject<List<Class1>>(response);
Reviews.ItemsSource = FSfeed;

关于c# - Windows 手机 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29681341/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com