将JSON文件解析为ListBox

时间:2012-11-25 22:26:04

标签: c# json windows-phone-7 parsing listbox

我正在尝试将JSON文件解析为ListBox,但我不知道如何这样做,并且在此处和其他地方难以理解示例。

目前,我有以下内容:

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

我试图做的是让一个ListBox显示来自JSON“TITLE”旁边的JSON的“IMAGE”。单击它们最终应该导航到包含所有其他信息的新页面,我可能会通过QueryStrings或其他东西传递所有的点点滴滴。一旦我弄明白这一点。

如果有人可以检查我的最后一点是否有意义,并指出我正确的方向为ListBox我很感激。

1 个答案:

答案 0 :(得分:1)

  1. 您应该删除SEASON2SEASON3类,并将SEASON1重命名为Episode。序列化中不使用类名。您还应该将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>