gpt4 book ai didi

c# - 反序列化嵌套的 JSON

转载 作者:太空宇宙 更新时间:2023-11-03 19:04:06 24 4
gpt4 key购买 nike

需要一双额外的眼睛来了解我在这里做错了什么。帮助表示赞赏。(包括批评)我对 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>));

当我这样做时,出现如下所示的错误,

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.

我在这里做错了什么?本质上我想做的是将事件作为对象列表,仅此而已。

最佳答案

在这里工作正常:

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而不是 List<PopularEvents>如@Me.Name 所述。

关于c# - 反序列化嵌套的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31239066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com