Get nested Json

CaliphCaliph Member ✭✭
edited March 2018 in Xamarin.Forms

hi guys
I want to get nested json and use them in listview
this is my property class

 public class Items
    {
        public Image  image { get; set; }
        public Artist artist { get; set; }
    }


    public class Rootobject
    {
        public Tracks tracks { get; set; }
    }

    public class Tracks
    {
        public Track[] track { get; set; }
        public Attr attr { get; set; }
    }

    public class Attr
    {
        public string page { get; set; }
        public string perPage { get; set; }
        public string totalPages { get; set; }
        public string total { get; set; }
    }

    public class Track
    {
        public string name { get; set; }
        public string duration { get; set; }
        public string playcount { get; set; }
        public string listeners { get; set; }
        public string mbid { get; set; }
        public string url { get; set; }
        public Streamable streamable { get; set; }
        public Artist artist { get; set; }
        public Image[] image { get; set; }
    }

    public class Streamable
    {
        public string text { get; set; }
        public string fulltrack { get; set; }
    }



    public class Image
    {
        public string text { get; set; }
        public string size { get; set; }
    }

    public class Artist
    {
        public string name { get; set; }
        public string mbid { get; set; }
        public string url { get; set; }
    }

and this is my MainPage.cs
but this code is not working with nested json

private async void OnGetJson()
        {
            try
            {
                if (CrossConnectivity.Current.IsConnected)
                {

                    _activityIndicator.IsRunning = true;
                    var uri = new Uri("**************~~~~e7d9d6&format=json");
                    var client = new HttpClient();
                    var content = await client.GetStringAsync(uri);
                    string a = content.ToString();
                    var tr = JsonConvert.DeserializeObject<List<Items>>(a);
                    ObservableCollection<Items> ob = new ObservableCollection<Items>(tr);
                    lister.ItemsSource = ob;


                    int i = ob.Count;
                    if (i > 0)

                    {
                        _activityIndicator.IsRunning = false;
                    }
                    i = (ob.Count * heightRowsList);
                    _activityIndicator.HeightRequest = i;
                }
                else
                {
                    await DisplayAlert("Error", "No Connection", "Cancel");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

Can Anyone help?

Answers

  • N_BauaN_Baua INMember ✭✭✭✭✭

    Hi @Caliph ,
    I would say there are two ways to do it,
    One Group your data and use it in listview (some duplicate values would require in JSON).
    Read more here:

    Another way is have your data called as a flat JSON, however span the ids across multiple activity/pages like.
    For example if you have data like

    Category []
    --Sub-Category 1
    ----Product Type A []
    --Sub-Category 2
    ----Products Type B []

    Your page would use FLAT ListView in separate page/activity and pass id

    MainActivity --> SubCategoryActivity---> ProductActivity
    Category[] (pass cid: 1) ---> Sub-Category (pass scid: 101) ---> List products for sub category having id 101.

    Hope it helps.

    --N Baua

Sign In or Register to comment.