0
votes

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.

2
Can you please display the "Opening Hours" class? - Pramod
It's helpful if you provide actual runnable code, so complete classes, etc. - Nicholas Carey
I don't think you are show the code that actually throw the error. You error say you are trying to deserialize ''into type 'System.Collections.Generic.Dictionary`2'', there is no Dictionary type in your code at all - LeY

2 Answers

0
votes

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
}]

}

0
votes

Works on my machine, pretty much as-is. C#/.Net v4.7.1 on Windows 10. Newtonsoft.Json at v11.0.2.

About the only change I had to make was to make you classes' properties get/set rather than just get. Otherwise the deserializer can't assign a value to them when it rehydrates things.

using System;
using System.Collections.Generic;

using Newtonsoft.Json;

namespace ConsoleApp6
{
  class Program
  {
    const string json = @"
{
  ""address"": ""address"",
  ""id"": 1,
  ""latitude"": 46.0757062,
  ""longitude"": 18.1975697,
  ""name"": ""store name"",
  ""openingHours"":
    [
        { ""closeing"": ""01:00:00"", ""opening"": ""11:00:00"", ""weekday"": 1 }
    ]
}
";

    static void Main( string[] args )
    {
      Pub rehydrated;
      try
      {
        rehydrated = JsonConvert.DeserializeObject<Pub>( json );
      }
      catch ( Exception e )
      {
        Console.WriteLine( e.ToString() );
      }

      return;
    }

    public class Pub
    {
      [JsonProperty( "id" )]
      public int Id { get; set; }

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

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

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

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

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

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

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

    }

  }
}