[英]How to read JSON via FileStream?
I have a JSON with the following content:我有一个包含以下内容的 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"}
What I want is to get the values of the JSON (host, 123, etc)in my app.我想要的是在我的应用程序中获取 JSON(主机、123 等)的值。 What I have tried is:
我尝试过的是:
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";
}
}
Using this method, I get the following exception: Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List
1`.使用这种方法,我得到以下异常:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List
1`。 I know there's a topic about it, but it couldn't help solve the issue.我知道有一个关于它的话题,但它无助于解决问题。 What am I missing?
我错过了什么?
Your JSON contains a single object.您的 JSON 包含一个对象。 You're trying to deserialize it as a list of objects.
您正在尝试将其反序列化为对象列表。 That would only be valid if it started with
[
and ended with ]
.仅当它以
[
开头并以]
结尾时才有效。
Your commented out code tries to deserialize the right type, but using the result of MapPath
instead of the actual JSON.您注释掉的代码尝试反序列化正确的类型,但使用
MapPath
的结果而不是实际的 JSON。 I suggest you separate out the "loading the JSON" from the "deserializing the JSON":我建议您将“加载 JSON”与“反序列化 JSON”分开:
string file = HttpContext.Current.Server.MapPath("~/JSON/my-download.json");
string json = File.ReadAllText(file);
Root root = JsonConvert.DeserializeObject<Root>(json);
I'd also strongly recommend renaming Root
to a more meaningful name, eg ServerSettings
or SmtpSettings
- and rename the properties to be more conventional in .NET, using [JsonProperty]
to specify the name in the JSON.我还强烈建议将
Root
重命名为更有意义的名称,例如ServerSettings
或SmtpSettings
- 并将属性重命名为在 .NET 中更常规,使用[JsonProperty]
在 JSON 中指定名称。 For example, you could have:例如,您可以:
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; }
}
I would note that this is very weird JSON though, including spaces and colons in the property names.我会注意到这是非常奇怪的 JSON,包括属性名称中的空格和冒号。 It would be rather more conventional as:
它会更传统,因为:
{
"smtpHost": "...",
"smtpPort": ...,
"smtpUserName": "...",
"smtpUserPassword": "...",
"smtpFromAddress": "...",
"smtpDisplayName": "..."
}
... ideally without the smtp
prefix, admittedly... and with the port specified as a number rather than as a string. ... 无可否认,理想情况下没有
smtp
前缀...并且端口指定为数字而不是字符串。 Including a plain-text password is "unfortunate" in terms of security, too.就安全性而言,包含纯文本密码也是“不幸的”。
Basically, if you can possibly change everything about this system, there's a lot that should be changed... but at least the above should get you started.基本上,如果您可以更改有关此系统的所有内容,则应该更改很多内容……但至少以上内容可以帮助您入门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.