[英]Deserializing nested JSON
需要額外的眼睛來看看我在這里做錯了什么。 非常感謝您的幫助。(包括批評)我對Json來說還很陌生,所以請放輕松。
雖然只是在使用BandsInTown API,但我發現反序列化此json數據很奇怪。
我不確定在此處粘貼Json結果的最佳方法是什么,因此請單擊下面的鏈接以查看數據。
這就是我想要做的
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>));
當我這樣做時,出現如下所示的錯誤,
類型'Newtonsoft.Json.JsonSerializationException'的第一次機會異常發生在Newtonsoft.Json.DLL中,無法將當前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());
基本上, json
都是關於PopularEvents
而不是@ Me.Name提到的List<PopularEvents>
。
剛剛在本地嘗試過,肯定可以正常工作:
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; }
}
然后調用:
var result = JsonConvert.DeserializeObject<PopularEvents>(json)
反序列化沒有問題。
只需從您的代碼更改行:
var events = (List<PopularEvents>)JsonConvert.DeserializeObject(jsonMessage, typeof(List<PopularEvents>));
通過以下方式:
var events = JsonConvert.DeserializeObject<PopularEvents>(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;
string json = new StreamReader(res.Content.ReadAsStreamAsync().Result).ReadToEnd();
var events = JsonConvert.DeserializeObject<PopularEvents>(json);
而且一切正常
這是因為JSON的根元素是數據,它是關於PopularEvents類的,而不是有關List <PopularEvents>的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.