I have Generic List
GetAllSponsor sponsorlist = new GetAllSponsor();
sponsorlist.getallsponsor();
string spnsrlst = sponsorlist.ToFormattedJsonString();
Sponsors sponsorObjectData = JsonConvert.DeserializeObject<Sponsors>(spnsrlst);
Here is Sponsors Class definition looks like:
public class Sponsors
{
public string id { get; set; }
public string company_name { get; set; }
public string description { get; set; }
public string sponsor_level { get; set; }
public string picture { get; set; }
public string location { get; set; }
public string website_url { get; set; }
public string sm_twitter { get; set; }
public string sm_facebook { get; set; }
public string sm_linkedin { get; set; }
public string sm_pinterest { get; set; }
public string contact_number { get; set; }
public string attachments { get; set; }
public string date_time { get; set; }
}
I need to pass it to Observable Collection.
How I can do this?
List<SomeType> list = new List<SomeType>();
// add items to list
ObservableCollection<SomeType> collection = new ObservableCollection<SomeType>(list);
To summarize our chat, I would move the download code out of that class. The class should be called Sponsor (since it represents a single sponsor), and it should just hold public properties for the data fields that you are receiving (ID, company_name, description, etc.) The JSON that you are getting is actually an array of Sponsors, so you need to deserialize it to a List<Sponsor> or something appropriate. Then, loop through your list, and add each sponsor to the ObservableCollection like this:
foreach (Sponsor s in mySponsors)
{
myCollection.Add(s);
}
Related
So, I've made classes like this:
public class Values
{
public string odata_context { get; set; }
public List<ContactsDeserialize> keyValues { get; set; }
}
public class ContactsDeserialize : IDisposable
{
public string odata_etag { get; set; }
public Guid contactid { get; set; }
public string crimson_title { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public DateTime? createdon { get; set; }
public DateTime? birthdate { get; set; }
public string emailaddress1 { get; set; }
public string mobilephone { get; set; }
public string address1_line1 { get; set; }
public string address1_line2 { get; set; }
public string address1_line3 { get; set; }
public string address1_city { get; set; }
public string address1_postalcode { get; set; }
public string address1_telephone1 { get; set; }
}
but when I try to deserialise
this by using
Values ContactData = JsonConvert.DeserializeObject<Values>(jsonstring);
it's return null.
Can not find out the reason
Thanks
Your class fields a little bit differ to original json fields, so if you want to use this you need to put attributes to your fields to provide correct json fields i.e
[JsonProperty("#odata.etag")]
public string odata_etag { get; set; }
for more details see the documentation article
Could be that you need to intialise the keyValues list in a constructor.
public class Values
{
public Values()
{
keyValues = new List<ContactsDeserialize>();
}
public string odata_context {get; set; }
public List<ContactsDeserialize> keyValues { get; set; }
}
Alos the names of you objects should match exactly what the json return unless you define what it maps to in an attribute. Both odata_context and keyValues are named slightly different to how they are returned in the json.
You declare public List<ContactsDeserialize> keyValues { get; set; } in Class Values,but the node of your json is value.
This object fills just fine
[DelimitedRecord("|")]
public class LvMenuItems
{
public string Sequence { get; set; }
public string DisplayText { get; set; }
public string MenuId { get; set; }
public string PageUrl { get; set; }
public string ControlPanelSequence { get; set; }
}
[DelimitedRecord("|")]
public class LvMenuItems
{
public string Sequence { get; set; }
public string DisplayText { get; set; }
public string MenuId { get; set; }
public string PageUrl { get; set; }
public string ControlPanelSequence { get; set; }
[LVMenuItems]
public LVMenutItems menu items {get; set;}
}
Is it possible to embed a copy of the same structure as an attribute of an object to introduce recurrsion?
I was going about this completely wrong. The correct way it to declare a single object that corresponds to the delimited file and then use that object to fill a second object that handles recursion.
I have the following data structure
public class Prediction
{
public string Description {get;set;}
public string Id { get; set; }
public List<MatchedSubstring> Matched_substrings { get; set; }
public string Place_id { get; set; }
public string Reference { get; set; }
public List<Term> Terms { get; set; }
public List<string> Types { get; set; }
}
public class GooglePlaceAutocompleteResult
{
public List<Prediction> Predictions { get; set; }
public string Status { get; set; }
}
What I would like to do is remove any items in the Predictions list if their Types collection contains the string "sublocality".
How can I do this with LINQ?
Assuming you are writing a method within GooglePlaceAutocompleteResult, you can write:
Predictions = Predictions.Where(p => !p.Types.Any(t => t.Contains("sublocality")).ToList();
Otherwise the code would be the same but would be result.Predictions = result.Predictions...
I'm trying to fetch a name from one iList into another where they both contain the same ID.
public class Notifications
{
[JsonProperty("note_id")]
public int note_id { get; set;}
[JsonProperty("sender_id")]
public int sender_id { get; set;}
public string sender_name { get; set; }
[JsonProperty("receiver_id")]
public int receiver_id { get; set; }
[JsonProperty("document_id")]
public int document_id { get; set; }
[JsonProperty("search_name")]
public string search_name { get; set; }
[JsonProperty("unread")]
public int unread { get; set; }
}
public class CompanyDirectory
{
[JsonProperty("contact_id")]
public int contact_id { get; set; }
[JsonProperty("first_name")]
public string first_name { get; set; }
[JsonProperty("second_name")]
public string second_name { get; set; }
[JsonProperty("extension")]
public string extension { get; set; }
[JsonProperty("direct_dial")]
public string direct_dial { get; set; }
[JsonProperty("job_title")]
public string job_title { get; set; }
[JsonProperty("company_id")]
public int company_id { get; set; }
}
Then i'm doing the following, the lists both get populated fine. The weird bit is i'm getting errors saying that where I do CompanyDir.first_name that the property doesn't exist, where it clearly does?:
// This occurs just after the class declaration
public IList<Notifications> Notes;
public IList<CompanyDirectory> CompanyDir;
// Ignore that these both use the same string they're created at different parts of the load process
CompanyDir = JsonConvert.DeserializeObject<IList<CompanyDirectory>>(responseString);
Notes = JsonConvert.DeserializeObject<IList<Notifications>>(responseString);
// Now I thought I should be able to do
foreach(var s in Notes){
var thename = CompanyDir.first_name.Where(contact_id.Contains(s.sender_id))
}
You should look up LinQ and Lambda expressions:
var firstNames = CompanyDir.Where(c => c.contact_id == s.sender_id)
.Select(c => c.first_name)
.ToList();
Now you have a list of firstnames. A list, because there may be zero or more hits for your Where constraint.
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?
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 ...
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.