c反序列化json数组和数据绑定



我很难反序列化这个json文件。我想将"favoriteSongs"数组绑定到xaml中的视图,并在长列表选择器中显示所有信息。

{
    "_id": "56fd864ff28510694a6a7b5e",  
    "addFavorites": {
          "favoriteSongs": [
                   {
                           "_id": "570aeb89869a0520e0d0365c",
                           "title": "get lucky",
                           "album": "random access memory",
                           "primaryArtist": "daft punk",
                           "artist": "570ae933cd7d1072b430ea51",
                           "primaryGenre": "electro",
                           "genre": "570ae933cd7d1072b430ea52",
                           "duration": "3m31s",
                           "year": 2013
                   },
                   { ... }
          ]
}

在我的视图模型中,我使用observalecollection添加json中的信息,这里的"getFavoriteSongsModel"是我的RootObject

for (int i = 0; i < count; i++)
        {
          getFavoriteSongsModel = JsonConvert.DeserializeObject<GetFavoriteSongsModel>(e.Result);
          getFavoriteSongs.Add(new GetFavoriteSongsModel()
            {
                Id = getFavoriteSongsModel.Id,
                Favorites = getFavoriteSongsModel.Favorites
            });

我试图将这个模式与另一个observalecollection一起使用,该collection指向一个类,该类包含我需要的所有信息,但每次都会崩溃。我不知道如何使用"收藏夹"变量,我知道它已填充,但我无法在xaml中显示我想要的内容。

            <Grid>
                <phone:LongListSelector x:Name="MainLongListSelector" Margin="0,0,-12,0" ItemsSource="{Binding getFavoriteSongs}">
                    <phone:LongListSelector.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,17" >
                                <TextBlock Text="{Binding FavoriteSongs.Album}" Style="{StaticResource PhoneTextBlockBase}"/>
                                <TextBlock Text="{Binding FavoriteSongs.Title}" Style="{StaticResource PhoneTextBlockBase}"/>
                            </StackPanel>
                        </DataTemplate>
                    </phone:LongListSelector.ItemTemplate>
                </phone:LongListSelector>
            </Grid>

我根据自己的理解创建了一个示例代码。

我已经在json2csharp的Json字符串的基础上创建了三个类,这对于在Json字符串基础上创建类非常有用。

三个等级如下

 public class FavoriteSong
{
    public string _id { get; set; }
    public string title { get; set; }
    public string album { get; set; }
    public string primaryArtist { get; set; }
    public string artist { get; set; }
    public string primaryGenre { get; set; }
    public string genre { get; set; }
    public string duration { get; set; }
    public int year { get; set; }
}
public class AddFavorites
{
    public ObservableCollection<FavoriteSong> favoriteSongs { get; set; }
}
public class MainClass
{
    public string _id { get; set; }
    public AddFavorites addFavorites { get; set; }
}

我刚刚将您的字符串用作json,并在此基础上创建了这个samle。

以下代码将反序列化json字符串

   public MainClass objMainClass { get; set; }
  string strJson = "{"_id": "56fd864ff28510694a6a7b5e","addFavorites": {"favoriteSongs": [{"_id": "570aeb89869a0520e0d0365c","title": "get lucky","album": "random access memory","primaryArtist": "daft punk","artist": "570ae933cd7d1072b430ea51","primaryGenre": "electro","genre": "570ae933cd7d1072b430ea52","duration": "3m31s","year": 2013}]}}";
        objMainClass = new MainClass();
        objMainClass = JsonConvert.DeserializeObject<MainClass>(strJson);
        MainLongListSelector.DataContext = objMainClass.addFavorites;

XAML中的绑定如下

<phone:LongListSelector
        x:Name="MainLongListSelector"
        Margin="0,0,-12,0"
        ItemsSource="{Binding favoriteSongs}">
        <phone:LongListSelector.ItemTemplate>
            <DataTemplate>
                <StackPanel
                    Margin="0,0,0,17">
                    <TextBlock
                        Text="{Binding album}"
                        Style="{StaticResource PhoneTextBlockBase}" />
                    <TextBlock
                        Text="{Binding title}"
                        Style="{StaticResource PhoneTextBlockBase}" />
                </StackPanel>
            </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
    </phone:LongListSelector>

希望对你有帮助。

相关内容

  • 没有找到相关文章

最新更新