如何序列化为这种特定的 JSON 格式
How to serialize to this specific JSON format
我有一个收件人列表作为列表。如何将 c# 对象序列化为 mailgun 请求的这种特定 JSON 格式?
C#
var recipients = new List<Recipient>
{
new Recipient("test1@foo.com", "Foo Bar 1", "1234"),
new Recipient("test2@foo.com", "Foo Bar 2", "9876"),
...
}
预计JSON(根据https://documentation.mailgun.com/user_manual.html#batch-sending)
{
"test1@foo.com": { "name": "Foo Bar 1", "customerNumber": "1234" },
"test2@foo.com": { "name": "Foo Bar 2", "customerNumber": "9876" },
}
使用 JsonObject
和可序列化方法 SimgpleJson.SerializeObject()
将生成 JSON 如下所示:
{
[
{"test1@foo.com": { "name": "Foo Bar 1", "customerNumber": "1234" }},
{"test2@foo.com": { "name": "Foo Bar 2", "customerNumber": "9876" }},
]
}
实际结果是正确的。
收件人列表立即转换为数组构造 [],因此此列表中的任何内容都将显示为实际输出。
你的期望是行不通的,因为它会反序列化为:
public class Rootobject
{
public Test1FooCom test1foocom { get; set; }
public Test2FooCom test2foocom { get; set; }
}
public class Test1FooCom
{
public string name { get; set; }
public string customerNumber { get; set; }
}
public class Test2FooCom
{
public string name { get; set; }
public string customerNumber { get; set; }
}
你应该使用字典来表示预期的 JSON,像这样:
var recipients = new Dictionary<string, Recipient>
{
{"test1@foo.com", new Recipient("Foo Bar 1", "1234")},
{"test2@foo.com", new Recipient("Foo Bar 2", "9876")},
...
}
或这个:
var recipients = new Dictionary<string, object>
{
{"test1@foo.com", new {name = "Foo Bar 1", customerNumber = "1234"}},
{"test2@foo.com", new {name = "Foo Bar 2", customerNumber = "9876"}}
};
Debug.WriteLine(recipients.ToJson());
我认为你可以使用下面的class来序列化对象
public class Test1FooCom
{
public string name { get; set; }
public string customerNumber { get; set; }
}
var obj = new Dictionary<string, Test1FooCom>
{
{"test1@foo.com", new Test1FooCom(){name="Foo Bar 1",customerNumber="1234"}},
{"test2@foo.com", new Test1FooCom(){name="Foo Bar 2",customerNumber="9876"}},
};
var json = JsonConvert.SerializeObject(obj);
输出Json
{
"test1@foo.com":{
"name":"Foo Bar 1",
"customerNumber":"1234"
},
"test2@foo.com":{
"name":"Foo Bar 2",
"customerNumber":"9876"
}
}
我有一个收件人列表作为列表。如何将 c# 对象序列化为 mailgun 请求的这种特定 JSON 格式?
C#
var recipients = new List<Recipient>
{
new Recipient("test1@foo.com", "Foo Bar 1", "1234"),
new Recipient("test2@foo.com", "Foo Bar 2", "9876"),
...
}
预计JSON(根据https://documentation.mailgun.com/user_manual.html#batch-sending)
{
"test1@foo.com": { "name": "Foo Bar 1", "customerNumber": "1234" },
"test2@foo.com": { "name": "Foo Bar 2", "customerNumber": "9876" },
}
使用 JsonObject
和可序列化方法 SimgpleJson.SerializeObject()
将生成 JSON 如下所示:
{
[
{"test1@foo.com": { "name": "Foo Bar 1", "customerNumber": "1234" }},
{"test2@foo.com": { "name": "Foo Bar 2", "customerNumber": "9876" }},
]
}
实际结果是正确的。 收件人列表立即转换为数组构造 [],因此此列表中的任何内容都将显示为实际输出。
你的期望是行不通的,因为它会反序列化为:
public class Rootobject
{
public Test1FooCom test1foocom { get; set; }
public Test2FooCom test2foocom { get; set; }
}
public class Test1FooCom
{
public string name { get; set; }
public string customerNumber { get; set; }
}
public class Test2FooCom
{
public string name { get; set; }
public string customerNumber { get; set; }
}
你应该使用字典来表示预期的 JSON,像这样:
var recipients = new Dictionary<string, Recipient>
{
{"test1@foo.com", new Recipient("Foo Bar 1", "1234")},
{"test2@foo.com", new Recipient("Foo Bar 2", "9876")},
...
}
或这个:
var recipients = new Dictionary<string, object>
{
{"test1@foo.com", new {name = "Foo Bar 1", customerNumber = "1234"}},
{"test2@foo.com", new {name = "Foo Bar 2", customerNumber = "9876"}}
};
Debug.WriteLine(recipients.ToJson());
我认为你可以使用下面的class来序列化对象
public class Test1FooCom
{
public string name { get; set; }
public string customerNumber { get; set; }
}
var obj = new Dictionary<string, Test1FooCom>
{
{"test1@foo.com", new Test1FooCom(){name="Foo Bar 1",customerNumber="1234"}},
{"test2@foo.com", new Test1FooCom(){name="Foo Bar 2",customerNumber="9876"}},
};
var json = JsonConvert.SerializeObject(obj);
输出Json
{
"test1@foo.com":{
"name":"Foo Bar 1",
"customerNumber":"1234"
},
"test2@foo.com":{
"name":"Foo Bar 2",
"customerNumber":"9876"
}
}