加载JSON文件会出现序列化错误

时间:2018-10-11 07:56:21

标签: c# json

我有下面的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 \ nPath”,第1行,位置   1.“} Newtonsoft.Json.JsonSerializationException

我还需要序列化什么?

3 个答案:

答案 0 :(得分:7)

您的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());

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

答案 1 :(得分:1)

您的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());

答案 2 :(得分:0)

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

  

[{       “ applicationConfig”:{         “ Name”:“ Name1”,         “ Site”:“ Site1”       },       “ pathConfig”:{         “ SourcePath”:“ C:\ Temp \ Outgoing1”,         “ TargetPath”:“ C:\ Files”       },       “ credentialConfig”:{         “用户名”:“ test1”,         “密码”:“ super1”       }},{       “ applicationConfig”:{         “ Name”:“ Name2”,         “ Site”:“ Site2”       },       “ pathConfig”:{         “ SourcePath”:“ C:\ Temp \ Outgoing2”,         “ TargetPath”:“ C:\ Files”       },       “ credentialConfig”:{         “用户名”:“ test2”,         “密码”:“ super2”       }} [ <-HERE