Listview from WebRequest JSON

Hello everyone! Help to fill the List from the JSON request. I took the code from the example how I fill in List l from the request JSON

    public class DataSource
    {
        public string Name { get; set; }
        public string Category { get; set; }
        public string ImageFilename { get; set; }

        public DataSource ()
        {
        }

        public DataSource (string name, string category, string imageFilename)
        {
            Name = name;
            Category = category;
            ImageFilename = imageFilename;
        }

        public static List<DataSource> GetList (){

            var l = new List<DataSource> ();


            l.Add (new DataSource ("Asparagus","Vegetables","Vegetables"));
            l.Add (new DataSource ("Avocados","Vegetables","Vegetables"));
            l.Add (new DataSource ("Beetroots","Vegetables","Vegetables"));
            l.Add (new DataSource ("Capsicum","Vegetables","Vegetables"));
            l.Add (new DataSource ("Broccoli","Vegetables","Vegetables"));
            l.Add (new DataSource ("Brussel sprouts","Vegetables","Vegetables"));   
            return l;
        }
    }

Here is the url which returns JSON
http://api.yandexzaim.ru/api/menuToday

Posts

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    Sorry if I come across like a smart-ass but; what have you tried for yourself? And what do you think you should do?
    We could just type it out for you but that won't learn you much.

    Have you tried anything yourself? What did you run into?

  • JacobPoulsen_JacobPoulsen_ DKMember ✭✭✭

    Hi.

    Go to a website like http://jsonutils.com and post your JSON response to generate appropriate C# classes.

    Use a HttpClient to GET data from your API:

    var httpClient = new HttpClient();
    var httpResponse = await httpClient.GetStringAsync ("urlToApi");

    Use JSON.NET (Nuget) to convert the JSON response to your newly created C# classes:

    C#RootElement root = JsonConvert.DeserializeObject<C#RootElement>(httpResponse);

    Please make an honest effort...

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I don't know if I have understand correctly what you need.

    The JSON connected to the url is something like

    public class RootObject
    {
        public int id { get; set; }
        public int bludo { get; set; }
        public string title { get; set; }
        public string url { get; set; }
        public double price { get; set; }
        public double gram { get; set; }
    }
    

    If you use Json.net, you have to write something like

            List< RootObject > list;
    
    
            list = JsonConvert.DeserializeObject<List< RootObject >> (MYJSONSTRING);
    
  • @Jacob_b said:
    Hi.

    Go to a website like http://jsonutils.com and post your JSON response to generate appropriate C# classes.

    Use a HttpClient to GET data from your API:

    var httpClient = new HttpClient();
    var httpResponse = await httpClient.GetStringAsync ("urlToApi");

    Use JSON.NET (Nuget) to convert the JSON response to your newly created C# classes:

    C#RootElement root = JsonConvert.DeserializeObject<C#RootElement>(httpResponse);

    Please make an honest effort...

    Thx!!!

Sign In or Register to comment.