JsonConvert.DeserializeObject <t>返回null

时间:2016-05-10 10:30:07

标签: c# json

我有一个应用程序,我想在json文件中存储一些电子邮件设置。

json文件如下所示:

{
"Id": 1,
"From": "myemail@whereever.com",
"Password": "mypassword",
"Port": 587,
"To": "myemail@whereever.com",
"SmtpAddress": "smtp.office365.com",
"TargetName": "STARTTLS/smtp.office365.com"
}

和C#代码如下:

public class EmailModel
{
    [JsonProperty(nameof(Id))]
    public int Id { get; set; }
    [JsonProperty(nameof(To))]
    public string To { get; set; }
    [JsonProperty(nameof(From))]
    public string From { get; set; }
    [JsonProperty(nameof(Port))]
    public int Port { get; set; }
    [JsonProperty(nameof(Password))]
    public string Password { get; set; }
    [JsonProperty(nameof(SmtpAddress))]
    public string SmtpAddress { get; set; }
    [JsonProperty(nameof(TargetName))]
    public string TargetName { get; set; }
}

并访问它看起来像:

public EmailModel EmailModel(int id)
{
    var filePath = AppDomain.CurrentDomain.BaseDirectory;

    using (var sr = new StreamReader(Path.Combine(filePath,"appsettings.json")))
    {
        string json = sr.ReadToEnd();
        return JsonConvert.DeserializeObject<EmailModel>(json);
    }
}

我见过很多帖子,但似乎都没有。我添加了

var settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;
在EmailModel类中

仍然我的所有值仍然为null。

更新 通过这个调试,我看到sr.ReadToEnd()返回:

 "{\r\n  \"EmailModel\": {\r\n    \"Id\": 1,\r\n    \"From\": \"myemail\",\r\n    \"Password\": \"myPassword\",\r\n    \"Port\": 587,\r\n    \"To\": \"myEmail\",\r\n    \"SmtpAddress\": \"smtp.office365.com\",\r\n    \"TargetName\": \"STARTTLS/smtp.office365.com\"\r\n  }\r\n}\r\n"

}

更新2: 我从appsetting.json得到了回复,当我从

更改代码时
var filePath = AppDomain.CurrentDomain.BaseDirectory;
            var streamPath = Path.Combine(filePath, "appsettings.json");
            string json = null;
            using (var sr = new StreamReader(streamPath))
            {
                json = sr.ReadToEnd();

            }

为:

        var json = @"
            {
            ""Id"": 1,
            ""From"": ""myEmail"",
            ""Password"": ""myPassword"",
            ""Port"": 587,
            ""To"": ""myEmail"",
            ""SmtpAddress"": ""smtp.office365.com"",
            ""TargetName"": ""STARTTLS/smtp.office365.com""
            }";

我的测试没有失败。

2 个答案:

答案 0 :(得分:2)

  

运行代码证明,没有问题   反序列化本身。

除非你在幕后做其他事。 此作品

private static void EmailModel()
{
   var json = @"
              {
              ""Id"": 1,
              ""From"": ""myemail@whereever.com"",
              ""Password"": ""mypassword"",
              ""Port"": 587,
              ""To"": ""myemail@whereever.com"",
              ""SmtpAddress"": ""smtp.office365.com"",
              ""TargetName"": ""STARTTLS/smtp.office365.com""
              }";
  EmailModel result = null;
  var settings = new JsonSerializerSettings();
  settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

  result = JsonConvert.DeserializeObject<EmailModel>(json, settings);

  Console.WriteLine(result.From);
}

正如记录所述,请尝试检查您的文件是否存在且指定的路径是否正确。

<强>更新

OP更新后,情况发生了变化,即将发布的JSON包含根元素(EmailModel),因此无法直接序列化。这个post包含更深入的解释。

很快,代码需要像这样改变才能工作:

    private static void EmailModel()
    {
        var json = "{\r\n  \"EmailModel\": {\r\n    \"Id\": 1,\r\n    \"From\": \"myemail\",\r\n    \"Password\": \"myPassword\",\r\n    \"Port\": 587,\r\n    \"To\": \"myEmail\",\r\n    \"SmtpAddress\": \"smtp.office365.com\",\r\n    \"TargetName\": \"STARTTLS/smtp.office365.com\"\r\n  }\r\n}\r\n";

        JToken root = JObject.Parse(json);
        JToken model = root["EmailModel"];

        var result = JsonConvert.DeserializeObject<EmailModel>(model.ToString());

        Console.WriteLine(result.From);
    }

答案 1 :(得分:1)

您的文件包含损坏的数据。 现在它包含:

{
    EmailModel:
    {
        // ...
        // properties here
        // ...
    }
}

而不是

{
    // ...
    // properties here
    // ...
}

<强>更新 如果是这样,请创建一个类:

public class EmailModelSerialize
{
    public EmailModel EmailModel { get; set; }
}

以这种方式序列化:

EmailModelSerialize model;
using (var sr = new StreamReader(Path.Combine(filePath, "appsettings.json")))
{
    string json = sr.ReadToEnd();
    model = JsonConvert.DeserializeObject<EmailModelSerialize>(json);
}