Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize from json where can be single T object or array of T into List<T> [duplicate]

I have the code like this:

var json = GetJsonData(path);
JObject event_dates_data = JObject.Parse(json);
var event_dates_list = JObject.Parse(event_dates_data["document"]["date"].ToString());
var event_dates = JsonConvert.DeserializeObject<List<EventDate>>(event_dates_list.ToString());

Json may contains an array objects (for example "date:[{},{},{}]") or only one (for example "date:{}")

Json looks like that:

{
"document": {
"result": "success",
"resultcode": "000000",
"note": null,
"totaldates": "1",
"date": {
  "date_id": "351314",
  "live": "n",
  "datestart": "2012-03-07",
  "dateend": "2015-03-07",
  "timestart": "12:00",
  "timeend": "14:00",
  "date_available": "10000"
}
}
}

Or:

{
"document": {
"result": "success",
"resultcode": "000000",
"note": null,
"totaldates": "4",
"date": [
  {
    "date_id": "346022",
    "live": "n",
    "datestart": "2011-02-19",
    "dateend": "2011-02-19",
    "timestart": "12:00",
    "timeend": "14:00",
    "date_available": "10000"
  },
  {
    "date_id": "346023",
    "live": "n",
    "datestart": "2011-02-20",
    "dateend": "2011-02-20",
    "timestart": "12:00",
    "timeend": "14:00",
    "date_available": "10000"
  },
  {
    "date_id": "346024",
    "live": "n",
    "datestart": "2011-02-21",
    "dateend": "2011-02-21",
    "timestart": "12:00",
    "timeend": "14:00",
    "date_available": "10000"
  },
  {
    "date_id": "546580",
    "live": "y",
    "datestart": "2015-08-15",
    "dateend": "2015-08-15",
    "timestart": "12:00",
    "timeend": "14:00",
    "date_available": "10000"
  }
]
}
}

I have the poco for the "date":

public class EventDate {

    [JsonProperty("date_id")]
    public string Id { get; set; }


    [JsonProperty("live")]
    [JsonConverter(typeof(AvailableForSalesFiledConverter))]
    public bool AvailableForSales { get; set; }


    [JsonProperty("datestart")]
    public string DateStart { get; set; }


    [JsonProperty("dateend")]
    public string DateEnd { get; set; }


    [JsonProperty("timestart")]
    public string TimeStart { get; set; }


    [JsonProperty("timeend")]
    public string TimeEnd { get; set; }


    [JsonProperty("date_available")]
    public int DateAvailable { get; set; }
}

So when I'm trying to deserialize I getting exception: "Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[TicketProvider.BrownPaperTickets.Entities.EventDate]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo 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.\r\nPath 'date_id', line 2, position 13." How can I get it to List?

like image 369
Vitach Avatar asked Nov 26 '22 07:11

Vitach


1 Answers

var json = GetJsonData(path);
JObject event_dates_data = JObject.Parse(json);
var event_dates_list = JObject.Parse(event_dates_data["document"]["date"].ToString());
event_dates_list = string.Format("[{0}]", event_dates_list.Trim('[', ']'));
var event_dates = JsonConvert.DeserializeObject<List<EventDate>>(event_dates_list.ToString());
like image 105
brz Avatar answered Feb 02 '23 00:02

brz