如何通过 FileStream 读取 JSON?

How to read JSON via FileStream?

我有一个 JSON,内容如下:

{"SMTP Host: ":"host","SMTP Port: ":"123","SMTP User Name: ":"ionut","SMTP User Password: ":"pass","SMTP From: ":"robert","SMTP Display Name: ":"aeg","d":"2022-05-25T11:24:06.459Z"}

我想要的是在我的应用程序中获取 JSON(主机、123 等)的值。我试过的是:

    public class Root
        { 
            public string smtpHost { get; set; }
            public string smtpPort { get; set; }
            public string smtpUserName { get; set; }
            public string smtpPassword { get; set; }
            public string smtpFrommtpHost { get; set; }
            public string smtpDisplayName { get; set; }
        }
    
    public class CustomDocumentOperationService : DocumentOperationService
        {
         DocumentOperationResponse SendEmail(MailAddressCollection recipients, string subject, string messageBody, Attachment attachment)
            {
                using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/JSON/my-download.json")))
                {
                    List<Root> contentJSON = JsonConvert.DeserializeObject<List<Root>>(sr.ReadToEnd());
                    System.Diagnostics.Debug.WriteLine("***************************************contentJSON: " + contentJSON + "***************************************");
                }
    
/*I have tried also with the following 2line, but I get the error
'Unexpected character encountered while parsing value: C. Path '', line 0, position 0.'*/
                //Root friends = JsonConvert.DeserializeObject<Root>(System.Web.HttpContext.Current.Server.MapPath("~/JSON/my-download.json"));
                //System.Diagnostics.Debug.WriteLine("***************************************contentJSON: " + friends + "***************************************");

                //Here I want to get the values from JSON:
                string SmtpHost = "mail.test.com";
                int SmtpPort = 112;
                string SmtpUserName = "ionut@test.com";
                string SmtpUserPassword = "blablabla";
                string SmtpFrom = "ionut@test.com";
                string SmtpFromDisplayName = "Ionut";
             }
          }

使用这种方法,我得到以下异常: Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List1`。我知道有 a topic ,但它无法帮助解决问题。我错过了什么?

您的 JSON 包含一个对象。您正在尝试将其反序列化为对象列表。只有当它以 [ 开头并以 ] 结束时才有效。

您注释掉的代码尝试反序列化正确的类型,但使用 MapPath 的结果而不是实际的 JSON。我建议您将“加载 JSON”与“反序列化 JSON”分开:

string file = HttpContext.Current.Server.MapPath("~/JSON/my-download.json");
string json = File.ReadAllText(file);
Root root = JsonConvert.DeserializeObject<Root>(json);

我还强烈建议将 Root 重命名为更有意义的名称,例如ServerSettingsSmtpSettings - 并将属性重命名为在 .NET 中更常规,使用 [JsonProperty] 指定 JSON 中的名称。例如,您可以:

public class SmtpSettings
{
    [JsonProperty("SMTP Host: ")]
    public string Host { get; set; }

    [JsonProperty("SMTP Port: ")]
    public string Port { get; set; }

    [JsonProperty("SMTP User Name: ")]
    public string UserName { get; set; }

    [JsonProperty("SMTP User Password: ")]
    public string UserPassword { get; set; }

    [JsonProperty("SMTP From: ")]
    public string FromAddress { get; set; }

    [JsonProperty("SMTP Display Name: ")]
    public string DisplayName { get; set; }
}

我会注意到,这 非常 很奇怪 JSON,包括 属性 名称中的空格和冒号。它会更传统:

{
    "smtpHost": "...",
    "smtpPort": ...,
    "smtpUserName": "...",
    "smtpUserPassword": "...",
    "smtpFromAddress": "...",
    "smtpDisplayName": "..."
}

... 理想情况下没有 smtp 前缀,无可否认...并将端口指定为数字而不是字符串。就安全性而言,包括 plain-text 密码也是“不幸的”。

基本上,如果您可以更改此系统的所有内容,那么应该更改 很多...但至少以上内容应该可以帮助您入门。