【问题标题】:Loading JSON file gives serialization error加载 JSON 文件会导致序列化错误
【发布时间】:2018-10-12 14:31:45
【问题描述】:

我有以下 JSON 文件,

[
  {
    "applicationConfig": {
      "Name": "Name1",
      "Site": "Site1"
    },
    "pathConfig": {
      "SourcePath": "C:\\Temp\\Outgoing1",
      "TargetPath": "C:\\Files"
    },
    "credentialConfig": {
      "Username": "test1",
      "password": "super1"
    }
  },
  {
    "applicationConfig": {
      "Name": "Name2",
      "Site": "Site2"
    },
    "pathConfig": {
      "SourcePath": "C:\\Temp\\Outgoing2",
      "TargetPath": "C:\\Files"
    },
    "credentialConfig": {
      "Username": "test2",
      "password": "super2"
    }
  }
]

以下是C#类结构,

public class Configurations
{
    public List<ApplicationConfig> ApplicationConfigs { get; set; }
    public List<PathConfig> PathConfigs { get; set; }
    public List<CredentialConfig> CredentialConfigs { get; set; }
}


public class ApplicationConfig
{
    public string Name { get; set; }
    public string Site { get; set; }
}

public class PathConfig
{
    public string SourcePath { get; set; }
    public string TargetPath { get; set; }
}

public class CredentialConfig
{
    public string Username { get; set; }
    public string password { get; set; }
}

现在尝试加载 JSON 并出现以下错误,

using (var streamReader = new StreamReader(@"./Config.json"))
        {
           var X = JsonConvert.DeserializeObject<Configurations>(streamReader.ReadToEnd());
        }

$exception {"无法反序列化当前的 JSON 数组(例如 [1,2,3]) 进入类型“ConsoleApp8.Configurations”,因为该类型需要 要反序列化的 JSON 对象(例如 {\"name\":\"value\"}) 正确。\r\n若要修复此错误,请将 JSON 更改为 JSON 对象(例如 {\"name\":\"value\"})或将反序列化类型更改为 实现集合接口的数组或类型(例如 ICollection, IList) 之类的可以从 JSON 反序列化的 List 大批。 JsonArrayAttribute 也可以添加到类型中以强制它 从 JSON 数组反序列化。\r\n路径 '',第 1 行,位置 1."}Newtonsoft.Json.JsonSerializationException

我还需要序列化什么?

【问题讨论】:

    标签: c# json


    【解决方案1】:

    您的 JSON 代表一个数组 - 尽管结束 [ 应该是 ]。但是您试图将其序列化为单个 Configurations 对象。此外,您似乎期望应用程序配置、路径配置和凭据配置的单独数组 - 而您的 JSON 显示一个对象数组,每个对象都包含这三个。

    我怀疑你想要:

    public class Configuration
    {
        [JsonProperty("applicationConfig")]
        ApplicationConfig ApplicationConfig { get; set; }
    
        [JsonProperty("pathConfig")]
        PathConfig PathConfig { get; set; }
    
        [JsonProperty("credentialConfig")]
        CredentialConfig CredentialConfig { get; set; }
    }
    
    // Other classes as before, although preferably with the password property more conventionally named
    

    然后使用:

    List<Configuration> configurations = 
        JsonConvert.DeserializeObject<List<Configuration>>(streamReader.ReadToEnd());
    

    然后您将获得一个配置对象列表,每个配置对象都包含三个“子配置”部分。

    【讨论】:

      【解决方案2】:

      您的 JSON 类定义很接近但不完全。而且最后一个[必须是]

      JSON 类定义是使用QuickType 创建的

       public partial class Configuration
      {
          [JsonProperty("applicationConfig")]
          public ApplicationConfig ApplicationConfig { get; set; }
      
          [JsonProperty("pathConfig")]
          public PathConfig PathConfig { get; set; }
      
          [JsonProperty("credentialConfig")]
          public CredentialConfig CredentialConfig { get; set; }
      }
      
      public partial class ApplicationConfig
      {
          [JsonProperty("Name")]
          public string Name { get; set; }
      
          [JsonProperty("Site")]
          public string Site { get; set; }
      }
      
      public partial class CredentialConfig
      {
          [JsonProperty("Username")]
          public string Username { get; set; }
      
          [JsonProperty("password")]
          public string Password { get; set; }
      }
      
      public partial class PathConfig
      {
          [JsonProperty("SourcePath")]
          public string SourcePath { get; set; }
      
          [JsonProperty("TargetPath")]
          public string TargetPath { get; set; }
      }
      

      最后你需要用序列化

      var config_list = JsonConvert.DeserializeObject<List<Configuration>>(streamReader.ReadToEnd());
      

      【讨论】:

        【解决方案3】:

        我认为这是一个错字,您打开方括号而不是在 JSON 文件中关闭它。

        [{ “应用程序配置”:{ “名称”:“名称1”, “站点”:“站点 1” }, “路径配置”:{ "SourcePath": "C:\Temp\Outgoing1", “目标路径”:“C:\文件” }, “凭据配置”:{ "用户名": "test1", “密码”:“super1” } }, { “应用程序配置”:{ “名称”:“名称2”, “站点”:“站点 2” }, “路径配置”:{ "SourcePath": "C:\Temp\Outgoing2", “目标路径”:“C:\文件” }, “凭据配置”:{ "用户名": "test2", “密码”:“super2” } } [

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-19
          • 2016-04-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多