私は Windows Phone アプリケーションを初めて使用します。次の URL から取得した JSON ファイルがあります。
http://www.krcgenk.be/mobile/json/request/news/
ここで、タイトルを Windows Phone のリストに表示したいと考えています。そのための次の XAML があります。
<Grid>
<ListBox x:Name="News" Height="532">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" Margin="0,0,12,0" />
<TextBlock Text="{Binding Description}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
ここで、タイトルと説明をリストに入れる方法を知る必要があります。Google で作業した後、JSON.net フレームワークを使用する必要があることがわかりました。これにより、次のコードが得られました。
var w = new WebClient();
Observable
.FromEvent<DownloadStringCompletedEventArgs>(w, "DownloadStringCompleted")
.Subscribe(r =>
{
var deserialized =
JsonConvert.DeserializeObject<List<News>>(r.EventArgs.Result);
PhoneList.ItemsSource = deserialized;
});
w.DownloadStringAsync(
new Uri("http://www.krcgenk.be/mobile/json/request/news/"));
また、ゲッターとセッターを使用してニュース クラスを作成します。しかし、ビルドして実行すると。次のエラーが表示されます。
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into
type 'System.Collections.Generic.List`1[KrcGenk.Classes.News]' because the
type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3])
or change the deserialized type so that it is a normal .NET type (e.g. not
a primitive type like integer, not a collection type like an array or
List<T>) that can be deserialized from a JSON object. JsonObjectAttribute
can also be added to the type to force it to deserialize from a JSON object.
Path 'news', line 1, position 8.
誰かが私を助けてくれることを願っていますか?