Deserialize json with arrays with newtonsoft

3494 views c#
0

I'm trying to deserialize the following json object with newtosoft json in c#:

{
  "address": "address",
  "id": 1,
  "latitude": 46.0757062,
  "longitude": 18.1975697,
  "name": "store name",
  "openingHours": [
    {
      "closeing": "01:00:00",
      "opening": "11:00:00",
      "weekday": 1
    }
  ]
}

My class looks like this:

    [PrimaryKey, JsonProperty("id")]
    public int Id { get; }

    [JsonProperty("name"), Column("name")]
    public string Name { get; }

    [JsonProperty("latitude"), Column("latitude")]
    public double Latitude { get; }

    [JsonProperty("longitude"), Column("longitude")]
    public double Longitude { get; }

    [JsonProperty("openingHours"), Ignore]
    public List<OpeningHours> Openinghours { get; set; }

OpeningHours class:

public class OpeningHours
{
    [JsonProperty("weekday")]
    public int Day { get; set; }

    [JsonProperty("opening")]
    public TimeSpan Open { get; set; }

    [JsonProperty("closeing")]
    public TimeSpan Close { get; set; }
}

Here is how did a try to deserialize:

var result = JsonConvert.DeserializeObject<T>(json); //<-- this one should be the correct
var result = JsonConvert.DeserializeObject<T[]>(json);
var result = JsonConvert.DeserializeObject<List<T>>(json);

Every time i get an error like this:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,xMarksThePub.Model.OpeningHours]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'openingHours', line 1, position 137.

Pub is the name of my class.

I'm dosen't know what am i doing wrong.

answered question

Can you please display the "Opening Hours" class?

If you don't tell us what surprised you, it will be very hard to help you out.

Sorry but I did not find any answer to my question on the proposed duplicate thread

The point of a duplicate is that the same answers will be given. Asking a duplicate question is not the best way to get the answer to a question that's already been asked; bounties are.

2 Answers

13

Your JSON is incorrect because you have extra , in it. Try this one:

{
"address": "address",
"id": 1,
"latitude": 46.0757062,
"longitude": 18.1975697,
"name": "store name",
"openingHours": [{
    "closeing": "01:00:00",
    "opening": "11:00:00",
    "weekday": 1
}]

}

posted this
5

Here is the answer: 1 1 2

This is telling me _var has two different scopes: class vs. static scope

posted this

Have an answer?

JD

Please login first before posting an answer.