我需要从这个http请求体json中制作FormUrlEncodedContent
:
{
"number": 123456,
"names": {
"firstName": "a",
"secondName": "b",
"age": 10
}
}
我可以从“一级”json这样做:
FormUrlEncodedContent content = new FormUrlEncodedContent(
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("number", "123"),
new KeyValuePair<string, string>("name", "John")
});
但我真的不知道如何制作下一个关卡。我希望它用于POST请求。
更新:
我用它来连接NodeJs服务器。有这个控制:
const {number, names} = this.request.body;
if (!number|| !names|| !names.firstName|| !names.secondName|| !names.age)
如果您将其发送到ASP.NET MVC服务或WebAPI服务,则标准模型绑定器能够反序列化发送的请求,如下所示:
FormUrlEncodedContent content = new FormUrlEncodedContent(
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("number", "123"),
new KeyValuePair<string, string>("name", "John"),
new KeyValuePair<string, string>("names.firstName", "a"),
new KeyValuePair<string, string>("names.secondName", "b"),
new KeyValuePair<string, string>("names.age", "10"),
});
我经常看到的另一个约定(不适用于ASP.NET MVC或WebAPI中的默认模型绑定器)是这样做的:
FormUrlEncodedContent content = new FormUrlEncodedContent(
new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("number", "123"),
new KeyValuePair<string, string>("name", "John"),
new KeyValuePair<string, string>("names[firstName]", "a"),
new KeyValuePair<string, string>("names[secondName]", "b"),
new KeyValuePair<string, string>("names[age]", "10"),
});