【问题标题】:Deserialize json with arrays with newtonsoft使用 newtonsoft 用数组反序列化 json
【发布时间】:2018-11-06 00:39:45
【问题描述】:

我正在尝试用 c# 中的 newtosoft json 反序列化以下 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
    }
  ]
}

我的班级是这样的:

    [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 类:

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

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

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

这是尝试反序列化的方法:

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

每次遇到这样的错误:

Newtonsoft.Json.JsonSerializationException:无法反序列化 当前 JSON 数组(例如 [1,2,3])转换为类型 'System.Collections.Generic.Dictionary`2[System.String,xMarksThePub.Model.OpeningHours]' 因为该类型需要一个 JSON 对象(例如 {"name":"value"}) 正确反序列化。要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为 实现集合接口的数组或类型(例如 ICollection, IList) 之类的可以从 JSON 反序列化的 List 大批。 JsonArrayAttribute 也可以添加到类型中以强制它 从 JSON 数组反序列化。路径“openingHours”,第 1 行,位置 137.

Pub 是我班级的名字。

我不知道我做错了什么。

【问题讨论】:

  • 能否请您显示“开放时间”课程?
  • 如果您提供实际可运行的代码,那么完整的类等会很有帮助。
  • 我认为您没有显示实际引发错误的代码。您错误地说您正在尝试将''反序列化为类型'System.Collections.Generic.Dictionary`2'',您的代码中根本没有字典类型

标签: c# json json.net


【解决方案1】:

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

}

【讨论】:

  • 你能分享你的OpeningHours
【解决方案2】:

几乎可以在我的机器上运行。 Windows 10 上的 C#/.Net v4.7.1。Newtonsoft.Json 在 v11.0.2。

关于我必须做的唯一改变是让你的类属性get/set 而不仅仅是get。否则,反序列化程序在重新水化事物时无法为它们分配值。

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

    }

  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2017-10-16
    • 2018-01-11
    • 1970-01-01
    • 2020-02-29
    相关资源
    最近更新 更多