我正在使用这个课程:
class message
{
public content Content { get; set; }
public from From { get; set; }
public personalizations Personalizations { get; set; }
}
public class content
{
public string type = "text/html";
public string value = "html";
}
public class from
{
public string email = "example@example.com";
public string name = "example";
}
public class personalizations
{
public List<to> tos { get; set; }
}
public class to
{
public string subject { get; set; }
public string email { get; set; }
}
我将班级讯息序列化为:
var msg = new message() { Content = new content() { type = "text/html", value = "html" },
From = new from() { email = "example@example.com", name = "example" },
Personalizations = new personalizations() { tos = new List<to>() { new to(), new to() } } };
var data = JsonConvert.SerializeObject(msg);
我试图获得每个父母的数组
json输出格式是
{
"Content": {
"type": "text/html",
"value": "html"
},
"From": {
"email": "example@example.com",
"name": "example"
},
"Personalizations":
{
"tos": [
{
"subject": null,
"email": null
},
{
"subject": null,
"email": null
}
]
}
}
但我确实想要这种格式:
{
"content": [
{
"type": "text/html",
"value": "Html"
}
],
"from": {
"email": "",
"name": ""
},
"personalizations": [
{
"subject": "",
"to": [ { "email": "" }]
},
{
"subject": "",
"to": [{ "email": "" }]
},
{
"subject": "",
"to": [{ "email": "" }]
}
]
}
我怎样才能将格式更改为最后一个?
提前致谢
编辑:
我想更改格式而不是值 例如:
在最后的json示例中,我有一个个性化的对象,它拥有多个json,但在第一个我只有一个对象
答案 0 :(得分:0)
根据您的最新评论,您想要更改输出格式。您可以通过将属性“public string subject”从“to”类移动到“personalizations”类来实现这一目的,如下所示:
https://dotnetfiddle.net/40nBnl
顺便说一下,你应该看看C# naming conventions
答案 1 :(得分:0)
您可以使用您想要的JSON并将其复制到剪贴板。然后,您可以从编辑菜单转到Visual Studio中的任何.cs文件,您可以展开&#34;粘贴特殊&#34;菜单。选择选项&#34;将JSON粘贴为类&#34;你明白了:
public class Rootobject
{
public Content[] content { get; set; }
public From from { get; set; }
public Personalization[] personalizations { get; set; }
}
public class From
{
public string email { get; set; }
public string name { get; set; }
}
public class Content
{
public string type { get; set; }
public string value { get; set; }
}
public class Personalization
{
public string subject { get; set; }
public To[] to { get; set; }
}
public class To
{
public string email { get; set; }
}