Question

I have the class "Alerts", which contains some returned information from Wunderground's API. I then have another class inside "Alerts", "Alert". My code looks like this:

public class Alerts
{
    public class Features
    {
        public int alerts { get; set; }
    }

    public class Response
    {
        public string version { get; set; }
        public string termsofService { get; set; }
        public Features features { get; set; }
    }

    public class ZONED
    {
        public string state { get; set; }
        public string ZONE { get; set; }
    }

    public class StormBased
    {
    }

    public class Alert
    {
        public string type { get; set; }
        public string description { get; set; }
        public string date { get; set; }
        public string date_epoch { get; set; }
        public string expires { get; set; }
        public string expires_epoch { get; set; }
        public string message { get; set; }
        public string phenomena { get; set; }
        public string significance { get; set; }
        public List<ZONED> ZONES { get; set; }
        public StormBased StormBased { get; set; }
    }

    public class RootObject
    {
        public Response response { get; set; }
        public string query_zone { get; set; }
        public List<Alert> alerts { get; set; }
    }
    public class AlertsUpdateState
    {
        public HttpWebRequest AsyncRequest { get; set; }
        public HttpWebResponse AsyncResponse { get; set; }
    }
}

I create a RootObject when the app starts, and later use JSON to empty values. The call returns "response", "query_zone", and "alerts". Now the last one is a List of type Alert, which contains type, description, etc. of an issued alert.

So I have this list stored as alertVar. This has several methods, including count. I can figure out how many alerts are issued, but I'm not sure how to move on.

How do I retrieve the string values (such as type) with this list?

Was it helpful?

Solution

Assuming alertVar is your list of Alert, you can do something like:

string some_string;
foreach (var alert in alertVar) 
{
    some_string += alert.type + ", ";
}

This will add all the types to a long string (some_string). You can do the same for whichever property you want ...

OTHER TIPS

foreach (var alert in alerts) 
{
   var type = alert.type;
   var description = alert.description
}

That is a basic example of how you use the item you are looping over.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top