我正在尝试使用C#客户端发出PUT请求,此请求中包含JSON数据。
我使用这个,我从这里得到:Passing values to a PUT JSON Request in C#
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(new
{
reg_FirstName = "Bob",
reg_LastName = "The Guy"
});
当然,Json字符串看起来像这样:
{
"reg_FirstName":"Bob",
"reg_LastName":"The Guy"
}
但是我如何创建像这样的JSON字符串:
{
"main": {
"reg_FirstName": "Bob",
"reg_LastName": "The Guy"
},
"others": [
{
"reg_FirstName": "Robert",
"reg_LastName": "The Guy"
},
{
"reg_FirstName": "Rob",
"reg_LastName": "The Guy"
}
]
}
答案 0 :(得分:4)
您可以使用相同的方式 - 动态对象,因此在您的情况下它将如下所示:
var serializer = new JavaScriptSerializer();
string json =
serializer.Serialize(
new {
main = new
{
reg_FirstName = "Bob",
reg_LastName = "The Guy"
},
others = new[]
{
new { reg_FirstName = "Bob", reg_LastName = "The Guy" },
new { reg_FirstName = "Bob", reg_LastName = "The Guy" }
}
}
);