反序列化嵌套的JSON

时间:2015-07-06 06:33:33

标签: c# json json.net

需要多一双眼睛,看看我在这里做错了什么。帮助赞赏。(包括批评)我对Json很新,所以请放轻松我。

在使用BandsInTown API时,我发现反序列化这个json数据很奇怪。

我不确定这里粘贴Json结果的最佳方法,所以请点击下面的链接查看数据。

https://app.bandsintown.com/events/just_announced?location=San+Francisco%2C+CA&radius=75&per_page=15&authenticate=false

这就是我要做的事情,

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>));

当我这样做时,我收到如下所示的错误,

  

类型的第一次机会异常   &#39; Newtonsoft.Json.JsonSerializationException&#39;发生在   Newtonsoft.Json.DLL无法反序列化当前的JSON对象(例如   {&#34; name&#34;:&#34; value&#34;})进入类型   &#39; System.Collections.Generic.List`1 [BandsInTown.Models.PopularEvents]&#39;   因为该类型需要一个JSON数组(例如[1,2,3])来反序列化   正确。要修复此错误,请将JSON更改为JSON数组   (例如[1,2,3])或更改反序列化类型以使其正常   .NET类型(例如,不是整数的基本类型,不是集合   类似于数组或List的类型,可以从JSON反序列化   宾语。 JsonObjectAttribute也可以添加到类型中以强制它   从JSON对象反序列化。

我在这里做错了什么?基本上我想要做的是将事件作为对象列表,这就是它。

3 个答案:

答案 0 :(得分:4)

在这里工作正常:

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>

答案 1 :(得分:1)

现在就在本地试过,它肯定有效:

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)

无问题地反序列化。

答案 2 :(得分:1)

只需更改代码中的一行:

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&lt; PopularEvents&GT;