Reputation: 1299
Need an extra pair of eyes to see what i am doing wrong here. Help appreciated.(including criticisms) I am quite new to Json so please go easy on me.
While just playing around with the BandsInTown API, i am finding it weird to deserialize this json data.
I am not sure whats the best way to paste a Json result here, so please click on the link below to see the data.
Here's what i am trying to do,
HttpResponseMessage response = await HttpManager.BitRequestManager.SendRequest(uri);
var jsonMessage = await HttpManager.BitResponseManager.ReadResponse(response);
Here's my models,
public class PopularEvents
{
[JsonProperty("data")]
public Data Data;
}
public class Data
{
public List<Events> events { get; set; }
}
public class Events
{
public string id { get; set; }
public string artist_event_id { get; set; }
public string title { get; set; }
public string datetime { get; set; }
public string formatted_datetime { get; set; }
public string formatted_location { get; set; }
public string ticket_url { get; set; }
public string ticket_type { get; set; }
public string ticket_status { get; set; }
public string on_sale_datetime { get; set; }
public string facebook_rsvp_url { get; set; }
public string description { get; set; }
//public List<Artist> artists { get; set; }
//public Venue venue { get; set; }
public string facebook_event_id { get; set; }
public int rsvp_count { get; set; }
public int? media_id { get; set; }
}
var events = (List<PopularEvents>)JsonConvert.DeserializeObject(jsonMessage, typeof(List<PopularEvents>));
When i do this, i get an error as shown below,
A first chance exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.DLL Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[BandsInTown.Models.PopularEvents]' 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) 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.
What am i doing wrong here? essentially what i am trying to do is take the events as a list of objects and thats pretty much it.
Upvotes: 1
Views: 1794
Reputation: 1396
Just change line from your code:
var events = (List<PopularEvents>)JsonConvert.DeserializeObject(jsonMessage, typeof(List<PopularEvents>));
By the following:
var events = JsonConvert.DeserializeObject<PopularEvents>(json);
I've just checked it in code:
var res = new HttpClient().GetAsync("https://app.bandsintown.com/events/just_announced?location=San+Francisco%2C+CA&radius=75&per_page=15&authenticate=false").Result;
string json = new StreamReader(res.Content.ReadAsStreamAsync().Result).ReadToEnd();
var events = JsonConvert.DeserializeObject<PopularEvents>(json);
And all works fine
It is because the root element of JSON is data and it is about PopularEvents classs, not about List< PopularEvents>
Upvotes: 1
Reputation: 27357
Just tried this locally now, it's definitely working:
public class Artist
{
public int id { get; set; }
public string name { get; set; }
public string url { get; set; }
public string image_url { get; set; }
public string thumb_url { get; set; }
public string large_image_url { get; set; }
public bool on_tour { get; set; }
public string events_url { get; set; }
public string sony_id { get; set; }
public int tracker_count { get; set; }
public bool verified { get; set; }
public int media_id { get; set; }
}
public class Venue
{
public string name { get; set; }
public string address { get; set; }
public string city { get; set; }
public string region { get; set; }
public string country { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
}
public class Event
{
public string id { get; set; }
public string artist_event_id { get; set; }
public string title { get; set; }
public string datetime { get; set; }
public string formatted_datetime { get; set; }
public string formatted_location { get; set; }
public string ticket_url { get; set; }
public string ticket_type { get; set; }
public string ticket_status { get; set; }
public string on_sale_datetime { get; set; }
public string facebook_rsvp_url { get; set; }
public string description { get; set; }
public List<Artist> artists { get; set; }
public Venue venue { get; set; }
public string facebook_event_id { get; set; }
public int rsvp_count { get; set; }
public int? media_id { get; set; }
}
public class Data
{
public List<Event> events { get; set; }
}
public class Pages
{
public int current_page { get; set; }
public int total_results { get; set; }
public int results_per_page { get; set; }
public string next_page_url { get; set; }
public object previous_page_url { get; set; }
}
public class PopularEvents
{
public Data data { get; set; }
public Pages pages { get; set; }
}
Then calling:
var result = JsonConvert.DeserializeObject<PopularEvents>(json)
Deserializes without a problem.
Upvotes: 1
Reputation: 3726
working fine here:
var res = new HttpClient().GetAsync("https://app.bandsintown.com/events/just_announced?location=San+Francisco%2C+CA&radius=75&per_page=15&authenticate=false").Result;
var t = JsonConvert.DeserializeObject<PopularEvents>(new StreamReader(res.Content.ReadAsStreamAsync().Result).ReadToEnd());
Basically the json
is all about PopularEvents
and not a List<PopularEvents>
as mentioned by @Me.Name.
Upvotes: 5