[英]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 (eg {"name":"value"}) into type 'System.Collections.Generic.List
1`。 我知道有一個關於它的話題,但它無助於解決問題。 我錯過了什么?
您的 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
重命名為更有意義的名稱,例如ServerSettings
或SmtpSettings
- 並將屬性重命名為在 .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
前綴...並且端口指定為數字而不是字符串。 就安全性而言,包含純文本密碼也是“不幸的”。
基本上,如果您可以更改有關此系統的所有內容,則應該更改很多內容……但至少以上內容可以幫助您入門。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.