需要一双额外的眼睛来看看我在这里做错了什么。感谢帮助。(包括批评)我对 Json 还很陌生,所以请对我宽容一些。
在使用 BandsInTown API 时,我发现反序列化此 json 数据很奇怪。
我不确定在此处粘贴 Json 结果的最佳方法是什么,因此请单击下面的链接查看数据。
这就是我想做的,
HttpResponseMessage response = await HttpManager.BitRequestManager.SendRequest(uri);
Run Code Online (Sandbox Code Playgroud)
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>));
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我收到如下所示的错误,
Newtonsoft.Json.DLL 中发生类型“Newtonsoft.Json.JsonSerializationException”的第一次机会异常无法将当前 JSON 对象(例如 {“name”:“value”})反序列化为类型“System.Collections.Generic.List`1 [BandsInTown.Models.PopularEvents]',因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,不是像这样的集合类型)数组或列表),可以从 JSON 对象反序列化。还可以将 JsonObjectAttribute 添加到类型中以强制其从 JSON 对象反序列化。
我在这里做错了什么?本质上我想做的就是将事件作为对象列表,仅此而已。
在这里工作正常:
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());
Run Code Online (Sandbox Code Playgroud)
基本上json都是关于PopularEvents而不是List<PopularEvents>@Me.Name 提到的。
| 归档时间: |
|
| 查看次数: |
1646 次 |
| 最近记录: |