1

I have a json string like this:

{"bwEventList": {

    "events": [

      {
        "summary" : "Leisure Classes Beginning Today",
        "subscriptionId" : "",
        "calPath" : "%2Fpublic%2Fcals%2FMainCal",
        "guid" : "CAL-02a786e6-45f207c3-0147-11c6cf10-000074e3bedework%40lsu.edu",
        "recurrenceId" : "",
        "link" : "",
        "eventlink" : "http://calendar.apps.lsu.edu/feeder/feeder/event/eventView.do?b=de&calPath=%2Fpublic%2Fcals%2FMainCal&guid=CAL-02a786e6-45f207c3-0147-11c6cf10-000074e3bedework%40lsu.edu&recurrenceId=",
        "status" : "CONFIRMED",
        "start" : {
          "allday" : "true",
          "shortdate" : "11/22/14",
          "longdate" : "November 22, 2014",
          "dayname" : "Saturday",
          "time" : "12:00 AM",
          "utcdate" : "20141122T060000Z",
          "datetime" : "20141122",
          "timezone" : ""
        },
        "end" : {
          "allday" : "true",
          "shortdate" : "11/22/14",
          "longdate" : "November 22, 2014",
          "dayname" : "Saturday",
          "time" : "12:00 AM",
          "utcdate" : "20141122T060000Z",
          "datetime" : "20141122",
          "timezone" : ""
        }

I wrote my models like this:

    public class Event
{
    [JsonProperty(PropertyName ="summary")]
    public string summary { get; set; }
    [JsonProperty(PropertyName ="subscriptionId")]
    public string subscriptionId { get; set; }
    [JsonProperty(PropertyName ="calPath")]
    public string calPath { get; set; }
    [JsonProperty(PropertyName ="guid")]
    public string guid { get; set; }
    [JsonProperty(PropertyName ="reccurenceId")]
    public string recurrenceId { get; set; }
    [JsonProperty(PropertyName ="link")]
    public string link { get; set; }
    [JsonProperty(PropertyName ="eventlink")]
    public string eventlink { get; set; }
    [JsonProperty(PropertyName ="status")]
    public string status { get; set; }
    [JsonProperty(PropertyName ="start")]
    public Start start { get; set; }
    [JsonProperty(PropertyName ="end")]
    public End end { get; set; }
    [JsonProperty(PropertyName ="location")]
    public Location location { get; set; }
    [JsonProperty(PropertyName ="contact")]
    public Contact contact { get; set; }
    [JsonProperty(PropertyName ="calendar")]
    public Calendar calendar { get; set; }
    [JsonProperty(PropertyName ="categories")]
    public List<string> categories { get; set; }
    [JsonProperty(PropertyName ="description")]
    public string description { get; set; }
    [JsonProperty(PropertyName ="cost")]
    public string cost { get; set; }

}

public class BwEventList
{
    public List<Event> events { get; set; }
}

    public class Start
    {
        [JsonProperty(PropertyName ="allday")]
        public string allday { get; set; }
        [JsonProperty(PropertyName ="shortdate")]
        public string shortdate { get; set; }
        [JsonProperty(PropertyName ="longdate")]
        public string longdate { get; set; }
        [JsonProperty(PropertyName ="dayname")]
        public string dayname { get; set; }
        [JsonProperty(PropertyName ="time")]
        public string time { get; set; }
        [JsonProperty(PropertyName ="utcdate")]
        public string utcdate { get; set; }
        [JsonProperty(PropertyName ="datetime")]
        public string datetime { get; set; }
        [JsonProperty(PropertyName ="timezone")]
        public string timezone { get; set; }
    }

    public class End
    {
        public string allday { get; set; }
        public string shortdate { get; set; }
        public string longdate { get; set; }
        public string dayname { get; set; }
        public string time { get; set; }
        public string utcdate { get; set; }
        public string datetime { get; set; }
        public string timezone { get; set; }
    }

Im trying to deserialize the string and then display each event in a list onto an XML(android). Below is how I get the string and deserialize it

public async Task<List<ReveilleApp.Core.Models.Event>> GetItems(string feedUrl)
    {
        try{
        using (WebClient webclient = new WebClient ()) {
            WebClient n = new WebClient ();

            var json =  await n.DownloadStringTaskAsync (feedUrl);

            var model = JsonConvert.DeserializeObject<List<ReveilleApp.Core.Models.Event>>(json);

            return model;
            }
        }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;

            }


        }

Items are returned to populate a listView. At the exception I get this error

    Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ReveilleApp.Core.Models.Event]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Ive tried many solutions but none work

2 Answers 2

3

Your Model is not matching the Json columns.

Leonel Corrected the Models:

But i would like to suggest few correction in your method as well:

public async Task<RootObject> GetItems(string feedUrl)
{
        try
        {
            using (WebClient webclient = new WebClient()) 
            {
                 var json =  await webclient.DownloadStringTaskAsync(feedUrl);
                 var model = JsonConvert.DeserializeObject<RootObject>(json);
                 return model;
            }
        }
        catch(Exception ex)
        {
                Console.WriteLine(ex.Message);
                return null;
        }
}

You could get it as a List as well.

When you call this method

var root = GetItems(string feedUrl) as RootObject; // Ex call for method
if(root != null && root.bwEventList != null && root.bwEventList.Count > 0) // Just checking the conditions to make sure everthing is OK.
{
     List<Event> eventsList = root.bwEventList.events; // Here you will get it as a list.
}
Sign up to request clarification or add additional context in comments.

3 Comments

no way to return it as list? I am trying to populate a ListView
Yes you can get it as a list as well. see my update.
both your answers helped! Thank you
2

Your model is the problem because it's different from your JSON, I recreate your mode.

Try to setup and use this model:

public class Start
{
    public string allday { get; set; }
    public string shortdate { get; set; }
    public string longdate { get; set; }
    public string dayname { get; set; }
    public string time { get; set; }
    public string utcdate { get; set; }
    public string datetime { get; set; }
    public string timezone { get; set; }
}

public class End
{
    public string allday { get; set; }
    public string shortdate { get; set; }
    public string longdate { get; set; }
    public string dayname { get; set; }
    public string time { get; set; }
    public string utcdate { get; set; }
    public string datetime { get; set; }
    public string timezone { get; set; }
}

public class Event
{
    public string summary { get; set; }
    public string subscriptionId { get; set; }
    public string calPath { get; set; }
    public string guid { get; set; }
    public string recurrenceId { get; set; }
    public string link { get; set; }
    public string eventlink { get; set; }
    public string status { get; set; }
    public Start start { get; set; }
    public End end { get; set; }
}

public class BwEventList
{
    public List<Event> events { get; set; }
}

public class RootObject
{
    public BwEventList bwEventList { get; set; }
}

3 Comments

yea that didn't work, the json string is much bigger than the example btw. Do i really need all the objects to deserialize? I first got my model from json2charp, but there are some weird attributes that gave me issues
@Cubatown You can remove the RootObject if you want and use BWEventList instead. you can also use [JsonProperty("name")] instead of [JsonProperty(PropertyName ="name")].
Got it working after RJK's answer, your models helped too. upvoted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.