简体   繁体   中英

Displaying JSON data from web-service on WP7

I'm trying to parse and display JSON data retrieved using a webclient in my app. Here is the json string I want to display

{"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":[]}]}

UPDATE

I am now getting a NullReferenceException when it gets to the foreach loop. For some reason it doesn't recognize "data"

    // 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);
        }
    }

Here are my Classes.

    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; }
    }

Going off of LB's updated class' what do I need to do to display textblocks of all the genres using the "name" field? Here is my xaml below.

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

Thanks again.

Here are the class definitions I used to deserialize your json string

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM