我正在尝试解析并显示在我的应用中使用webclient检索的JSON数据。这是我要显示的json字符串
{"metadata":{"page":1,"perPage":23,"count":23},"results":[{"id":9,"name":"Breaks","slug":"breaks","subgenres":[]},{"id":10,"name":"Chill Out","slug":"chill-out","subgenres":[]},{"id":12,"name":"Deep House","slug":"deep-house","subgenres":[]},{"id":16,"name":"DJ Tools","slug":"dj-tools","subgenres":[]},{"id":1,"name":"Drum & Bass","slug":"drum-and-bass","subgenres":[]},{"id":18,"name":"Dubstep","slug":"dubstep","subgenres":[]},{"id":17,"name":"Electro House","slug":"electro-house","subgenres":[]},{"id":3,"name":"Electronica","slug":"electronica","subgenres":[]},{"id":40,"name":"Funk \/ R&B","slug":"funk-r-and-b","subgenres":[]},{"id":49,"name":"Glitch Hop","slug":"glitch-hop","subgenres":[]},{"id":8,"name":"Hard Dance","slug":"hard-dance","subgenres":[]},{"id":2,"name":"Hardcore \/ Hard Techno","slug":"hardcore-hard-techno","subgenres":[]},{"id":38,"name":"Hip-Hop","slug":"hip-hop","subgenres":[]},{"id":5,"name":"House","slug":"house","subgenres":[]},{"id":37,"name":"Indie Dance \/ Nu Disco","slug":"indie-dance-nu-disco","subgenres":[]},{"id":14,"name":"Minimal","slug":"minimal","subgenres":[]},{"id":39,"name":"Pop \/ Rock","slug":"pop-rock","subgenres":[]},{"id":15,"name":"Progressive House","slug":"progressive-house","subgenres":[]},{"id":13,"name":"Psy-Trance","slug":"psy-trance","subgenres":[]},{"id":41,"name":"Reggae \/ Dub","slug":"reggae-dub","subgenres":[]},{"id":11,"name":"Tech House","slug":"tech-house","subgenres":[]},{"id":6,"name":"Techno","slug":"techno","subgenres":[]},{"id":7,"name":"Trance","slug":"trance","subgenres":[]}]}
更新
当我到达foreach循环时,我现在得到一个NullReferenceException。由于某种原因,它无法识别“数据”
// Deserialize the Data
void beatportTest_GetDataCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer serializer = null;
try
{
serializer = new DataContractJsonSerializer(typeof(ObservableCollection<Result>));
ObservableCollection<Result> data = serializer.ReadObject(e.Result) as ObservableCollection<Result>;
// foreach loop to display data
foreach (Result genre in data)
{
string name = genre.name;
listGenres.Items.Add(name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是我的课程。
public class Genres
{
public Metadata metadata { get; set; }
public List<Result> results { get; set; }
}
public class Metadata
{
public int page { get; set; }
public int perPage { get; set; }
public int count { get; set; }
}
public class Result
{
public int id { get; set; }
public string name { get; set; }
public string slug { get; set; }
// public List<object> subgenres { get; set; }
}
关闭L.B的更新课程'我需要做什么才能使用“名称”字段显示所有类型的文本块?这是我的下面的xaml。
<controls:PanoramaItem x:Name="genres" Header="genres">
<!--Single line list-->
<Grid
<ListBox x:Name="listGenres" Margin="0,95,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="432" Height="78">
<TextBlock Text="???" Style="{StaticResource PhoneTextExtraLargeStyle}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PanoramaItem>
再次感谢。
答案 0 :(得分:0)
以下是我用于反序列化json字符串的类定义
public class Genres
{
public Metadata metadata;
public Result[] results;
}
public class Metadata
{
public int page;
public int perPage;
public int count;
}
public class Result
{
public int id;
public string name;
public string slug;
//public subgenre[] subgenres;
}
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Genres));
Genres data = serializer.ReadObject(e.Result) as Genres;