Parsowanie json

0

Powiedzcie co robię źle.

public class Feeds
    {
        string created_at;
        int entry_id;
        int field1;

        public string Created_at
        {
            get
            {
                return created_at;
            }

            set
            {
                created_at = value;
            }
        }

        public int Entry_id
        {
            get
            {
                return entry_id;
            }

            set
            {
                entry_id = value;
            }
        }

        public int Field1
        {
            get
            {
                return field1;
            }

            set
            {
                field1 = value;
            }
        }
        
    }
public class FeedsCollection
    {
        private List<Feeds> feeds;

        public List<Feeds> Feeds
        {
            get
            {
                return feeds;
            }

            set
            {
                feeds = value;
            }
        }
    }
protected void Page_Load(object sender, EventArgs e)
        {
            using (var webClient = new WebClient())
            {
                string rawJSON = webClient.DownloadString("https://api.thingspeak.com/channels/618749/fields/1.json?results=");
                FeedsCollection feedsCollection = JsonConvert.DeserializeObject<FeedsCollection>(rawJSON);
                Console.Write(feedsCollection);
            }
        }

Po uruchomieniu feedsCollection = null

0

@Shark167: Przetestowałem na apce konsolowej i kod, który wstawiłeś deserializuje poprawnie. Dostałem FeedsCollection z listą 100 feedów, więc błąd może masz w innym miejscu?

0

Te klasy które wkleiłem to na razie jedyny kod, więc gdzie indziej błędu nie ma. Gdy odpalam to co powyżej cały czas jest null. Ale znalazłem takie coś:

+		Header	{InnerText = {System.Web.HttpException (0x80004005): Nie można pobrać wewnętrznej zawartości , ponieważ zawartość nie jest literałem.
   w System.Web.UI.HtmlControls.HtmlContainerControl.get_InnerHtml()
   w System.Web.UI.HtmlControls.HtmlContainerControl.get_InnerText()}}	System.Web.UI.HtmlControls.HtmlHead

Co to oznacza?

0

@some_ONE: to są moje klasy:

public class Channel
    {
        public int id { get; set; }
        public string name { get; set; }
        public string latitude { get; set; }
        public string longitude { get; set; }
        public string field1 { get; set; }
        public DateTime created_at { get; set; }
        public DateTime updated_at { get; set; }
        public int last_entry_id { get; set; }
    }

public class Feed
    {
        public DateTime created_at { get; set; }
        public int entry_id { get; set; }
        public string field1 { get; set; }
    }
public class RootObject
    {
        public Channel channel { get; set; }
        public List<Feed> feeds { get; set; }
    }

protected void Page_Load(object sender, EventArgs e)
        {
            using (var webClient = new WebClient())
            {

                string rawJson = webClient.DownloadString("https://api.thingspeak.com/channels/618749/feeds.json?results=");
                RootObject rootColletion = JsonConvert.DeserializeObject<RootObject>(rawJson);

                Console.WriteLine(rawJson);


            }
        }

i nic, nic mi nie wyświetla. Co robię źle? Jak dalej wsadzic te dane do listboxa?

0

Twój kod działa...

https://rextester.com/DXMVK30864

0

Konsola w visual studio jest pusta... Czy ktoś mógłby mi pomóc wrzucić to do listboxa? Próbuje coś brać z neta, ale nie idzie

public class lista
    {
        static lista()
        {
            using (var webClient = new WebClient())
            {

                string rawJson = webClient.DownloadString("https://api.thingspeak.com/channels/618749/feeds.json?results=");
                RootObject rootCollection = JsonConvert.DeserializeObject<RootObject>(rawJson);

                Console.WriteLine(rootCollection.channel.id);

                foreach (var item in rootCollection.feeds)
                {
                    Console.WriteLine(item.entry_id);
                }

                AllDane = rootCollection.feeds;
            }
        }

        
        private static List<RootObject> allDane;

        public static List<RootObject> AllDane
        {
            get
            {
                return allDane;
            }

            set
            {
                allDane = value;
            }
        }

        public static List<RootObject> GetDane()
        {
            return AllDane;
        }

    }

i przy konfiguracji listboxa nie ma odpowiedniej metody, chciałbym created_at wyswietlac w listboxie

0

Konsola w visual studio jest pusta

Do samego wypisania jakiejś informacji na ekran możesz użyć Debug.WriteLine (zamiast Console.WriteLine). Wtedy podczas działania programu dostaniesz to co sobie wypisałeś na oknie 'Output' (jeśli nie widzisz tego w trybie debug musisz uruchomić ten widok klikając View->Output).

Edit : Oczywiście to jest najbardziej przydatne gdy Twój program nie jest typowo konsolowy (czyli np jak u Ciebie WebForms). Oraz pamiętaj aby to zrobić w trybie Debug (a nie np. Release)

1 użytkowników online, w tym zalogowanych: 0, gości: 1