2012-11-25 93 views
1

我試圖解析一個JSON文件到一個列表框,但我不知道如何做,並一直難以理解這裏和其他地方的例子。解析JSON文件到列表框

目前,我有以下幾點:

XAML

<controls:Pivot Title="Episode Guide"> 
     <!--Pivot item one--> 
     <controls:PivotItem Header="season 1"> 
      <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
       <ListBox x:Name="Season1Box" Margin="0,0,0,0" VerticalAlignment="Top" Height="607" SelectionChanged="Season1Box_SelectionChanged"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
           <Image Source="{Binding Path=image1}"/> 
           <TextBlock Text="{Binding Path=title1}" TextWrapping="Wrap" Margin="10,10,0,0" FontSize="25" Foreground="White" /> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </Grid> 
     </controls:PivotItem> 

     <!--Pivot item two--> 
     <controls:PivotItem Header="season 2"> 
      <Grid x:Name="ContentPanel2" Grid.Row="1" Margin="12,0,12,0"> 
       <ListBox x:Name="Season2Box" Margin="0,0,0,0" VerticalAlignment="Top" Height="607" SelectionChanged="Season2Box_SelectionChanged"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
           <Image Source="{Binding Path=image2}"/> 
           <TextBlock Text="{Binding Path=title2}" TextWrapping="Wrap" Margin="10,10,0,0" FontSize="25" Foreground="White" /> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </Grid> 
     </controls:PivotItem> 

     <controls:PivotItem Header="season 3"> 
      <Grid x:Name="ContentPanel3" Grid.Row="1" Margin="12,0,12,0"> 
       <ListBox x:Name="Season3Box" Margin="0,0,0,0" VerticalAlignment="Top" Height="607" SelectionChanged="Season3Box_SelectionChanged"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
           <Image Source="{Binding Path=image3}"/> 
           <TextBlock Text="{Binding Path=title3}" TextWrapping="Wrap" Margin="10,10,0,0" FontSize="25" Foreground="White" /> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </Grid> 
     </controls:PivotItem> 
    </controls:Pivot> 

C#

private void EpisodeGuideStream() 
    { 
     var client = new WebClient(); 
     client.OpenReadCompleted += 
      (s, eargs) => 
      { 
       var serializer = new DataContractJsonSerializer(typeof(RootObject)); 
       if (eargs.Error != null) 
       { 
        if (eargs.Error.Message.Contains("NotFound")) 
        { 
         MessageBox.Show("Could not retrieve episode guide", "Error", MessageBoxButton.OK); 
        } 
        else 
        { 
         MessageBox.Show("Could not retrieve episode guide", "Error", MessageBoxButton.OK); 
        } 
       } 
       else 
       { 
        var episodeGuide = (RootObject)serializer.ReadObject(eargs.Result); 

        //I have no idea what I'm doing (Part 1) 
        List<string> episode1 = new List<string>(); 
        List<string> title1 = new List<string>(); 
        List<string> director1 = new List<string>(); 
        List<string> writer1 = new List<string>(); 
        List<string> airdate1 = new List<string>(); 
        List<string> sypnosis1 = new List<string>(); 
        List<string> image1 = new List<string>(); 

        foreach (var obj in episodeGuide.SEASON1) 
        { 
         //I have no idea what I'm doing (Part 2) 
         episode1.Add(obj.EPISODE); 
         title1.Add(obj.TITLE); 
         director1.Add(obj.DIRECTOR); 
         writer1.Add(obj.WRITER); 
         airdate1.Add(obj.AIRDATE); 
         sypnosis1.Add(obj.SYPNOSIS); 
         image1.Add(obj.IMAGE); 

        //  Season1Box.ItemsSource = figure out what goes here; 
        } 
        foreach (var obj2 in episodeGuide.SEASON2) 
        { 
         //   populate Season2Box 
        } 
        foreach (var obj3 in episodeGuide.SEASON3) 
        { 
         //   populate Season3Box 
        } 



       } 
      }; 
     var uri = new Uri(jsonfile); 
     client.OpenReadAsync(uri); 
    } 

    public class SEASON1 
    { 
     public string EPISODE { get; set; } 
     public string TITLE { get; set; } 
     public string DIRECTOR { get; set; } 
     public string WRITER { get; set; } 
     public string AIRDATE { get; set; } 
     public string SYPNOSIS { get; set; } 
     public string IMAGE { get; set; } 
    } 

    public class SEASON2 
    { 
     public string EPISODE { get; set; } 
     public string TITLE { get; set; } 
     public string DIRECTOR { get; set; } 
     public string WRITER { get; set; } 
     public string AIRDATE { get; set; } 
     public string SYPNOSIS { get; set; } 
     public string IMAGE { get; set; } 
    } 

    public class SEASON3 
    { 
     public string EPISODE { get; set; } 
     public string TITLE { get; set; } 
     public string DIRECTOR { get; set; } 
     public string WRITER { get; set; } 
     public string AIRDATE { get; set; } 
     public string SYPNOSIS { get; set; } 
     public string IMAGE { get; set; } 
    } 

    public class RootObject 
    { 
     public List<SEASON1> SEASON1 { get; set; } 
     public List<SEASON2> SEASON2 { get; set; } 
     public List<SEASON3> SEASON3 { get; set; } 
    } 

    private void Season1Box_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     //To eventually navigate to another page 
    } 

    private void Season2Box_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     //To eventually navigate to another page 
    } 

    private void Season3Box_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     //To eventually navigate to another page 
    } 

JSON文件被正確解析,在這裏我可以檢索的數據位,那裏,但我不知道如何將它們放在一個ListBox中。

我試圖做的是有一個列表框,顯示從JSON旁邊的「標題」的JSON「IMAGE」。點擊它們最終應該會導航到一個包含所有其他信息的新頁面,我可能會通過QueryStrings或其他東西傳遞所有的零碎信息,或者一旦我找到了答案。

如果有人可以檢查,看看我的最後一點是有道理的,並指向我的列表框的正確方向,我會很感激。

回答

1
  1. 你應該擺脫SEASON2SEASON3類,並將其重命名SEASON1Episode。序列化中不使用類名稱。您還應該將IMAGE屬性更改爲Uri,以便將其綁定到圖像。

    public class Episode 
    { 
        public string EPISODE { get; set; } 
        public string TITLE { get; set; } 
        public string DIRECTOR { get; set; } 
        public string WRITER { get; set; } 
        public string AIRDATE { get; set; } 
        public string SYPNOSIS { get; set; } 
        public Uri IMAGE { get; set; } 
    } 
    
    public class RootObject 
    { 
        public List<Episode> SEASON1 { get; set; } 
        public List<Episode> SEASON2 { get; set; } 
        public List<Episode> SEASON3 { get; set; } 
    } 
    
  2. 您可以綁定(或指定)列表到ListBox的ItemsSource屬性,它將會逐漸加入基於列表中的對象行。每個列表項都使用它們相應的對象作爲DataContext,並且可以綁定到它們的屬性。

    Season1Box.ItemsSource = root.SEASON1; 
    Season2Box.ItemsSource = root.SEASON2; 
    Season3Box.ItemsSource = root.SEASON3; 
    
  3. 要綁定控件的屬性,對象屬性,你可以使用語法Text="{Binding Path=MyProperty}"。您甚至可以處理嵌套的屬性,如MyProperty.OtherProperty

    <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
          <Image Source="{Binding Path=IMAGE}"/> 
          <TextBlock Text="{Binding Path=TITLE}" TextWrapping="Wrap" Margin="10,10,0,0" FontSize="25" Foreground="White" /> 
         </StackPanel> 
        </DataTemplate> 
    </ListBox.ItemTemplate> 
    
+0

良好,即工作。現在我想將信息推到一個新的頁面,目前我做了以下內容: 'code' 私人無效Season1Box_SelectionChanged(對象發件人,SelectionChangedEventArgs E) { 如果(Season1Box.SelectedIndex == -1 ) return; var myObj = new Episode(); // myObj = Season1Box.SelectedIndex; //需要在線以上工作 – ReignOfComputer

+0

'代碼' //接上一篇文章 NavigationService.Navigate(new Uri(「/ EpisodeDetails.xaml?season = 1&episode =」+ myObj.EPISODE +「&title =」+ myObj.TITLE +「&writer =」+ myObj.WRITER +「&airdate =」+ myObj.AIRDATE +「&director =」+ myObj.DIRECTOR +「&sypnosis =」+ myObj.SYPNOSIS,UriKind.Relative)); Season1Box.SelectedIndex = -1; } – ReignOfComputer

+0

這是一個Pastebin:http:// pastebin。com/JJX2fE6b 我現在有困難的是,我不知道如何設置我想要傳遞到下一頁的值。我認爲這個錯誤在我評論過的地方?非常感謝。 – ReignOfComputer