-1

私は 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.

誰かが私を助けてくれることを願っていますか?

4

2 に答える 2

0

このブログ投稿をご覧くださいhttp://dotnetbyexample.blogspot.gr/2012/01/json-deserialization-with-jsonnet.html

于 2012-12-22T16:24:16.907 に答える
0

URL はオブジェクトを返します (これがエラー メッセージの内容です)。したがって、それをリストに逆シリアル化することは想定されていません。逆シリアル化は次のようになります。

var deserialized =
                JsonConvert.DeserializeObject<NewsEntry>(r.EventArgs.Result);

NewsEntry にはニュースのリストが含まれている必要があります

class NewsEntry {
    public List<News> News { get; set; }
    public NewsEntry() {
        News = new List<News>();
    }
}

注: News クラスにはすべてのプロパティがあると仮定しました。たぶん、これを調整する必要があります。

于 2012-12-22T16:24:28.430 に答える