我試圖解析一個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或其他東西傳遞所有的零碎信息,或者一旦我找到了答案。
如果有人可以檢查,看看我的最後一點是有道理的,並指向我的列表框的正確方向,我會很感激。
良好,即工作。現在我想將信息推到一個新的頁面,目前我做了以下內容: 'code' 私人無效Season1Box_SelectionChanged(對象發件人,SelectionChangedEventArgs E) { 如果(Season1Box.SelectedIndex == -1 ) return; var myObj = new Episode(); // myObj = Season1Box.SelectedIndex; //需要在線以上工作 – ReignOfComputer
'代碼' //接上一篇文章 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
這是一個Pastebin:http:// pastebin。com/JJX2fE6b 我現在有困難的是,我不知道如何設置我想要傳遞到下一頁的值。我認爲這個錯誤在我評論過的地方?非常感謝。 – ReignOfComputer