如何在RestSharp中发布许多不同的字段名称?



如何在 RestSharp 中发布许多不同的字段名称? 下面是我尝试过的代码,会是这样还是其他方式。 这里的任何帮助将不胜感激。

JObject jsonPOST = new JObject();
jsonPOST.Add("VariableName1", "temp1" );
jsonPOST.Add("VariableName2", "temp2" );

JObject jsonPOST1 = new JObject();
jsonPOST1.Add("VariableName1", "temp1" );
jsonPOST1.Add("VariableName2", "temp2" );

JObject jsonPOST2 = new JObject();
jsonPOST1.Add("number1", "2" );
jsonPOST1.Add("number2", "4" );
restRequest.AddParameter("application/json", jsonPOST , ParameterType.RequestBody);
restRequest.AddParameter("application/json", jsonPOST1 , ParameterType.RequestBody);
restRequest.AddParameter("application/json", jsonPOST2 , ParameterType.RequestBody);

如何使用上述 RestSharp 结构发布这样的数据?

我希望这种格式的 POST 请求将被发送到 REST API。

"{
"FieldName1":{
"VariableName1": "temp1",
"VariableName2": "temp2",
},
"FieldName2":{
"VariableName1": "temp1",
"VariableName2": "temp2",
},
"FieldName3": {
"number1": "2",
"number2": "4",
}
}"

您需要将所有JObject组合在一个JObject上。

// Main JObject
var mainObj = new JObject();
// Syntax i love
var obj1 = new JObject
{
{"name", "CorrM"},
{"ip", "127.0.0.1"}
};
// Syntax are accepted too
var obj2 = new JObject();
obj2.Add("bla1", "bla");
obj2.Add("bla2", "bla");
// Combine on one JObject
mainObj.Add("FieldName1", obj1);
mainObj.Add("FieldName2", obj2);

然后添加到正文(转换为字符串(。 (我不知道设置身体的正确方法(

restRequest.AddParameter("application/json", mainObj.ToString(Formatting.None), ParameterType.RequestBody);

输出字符串必须如下所示

{
"FieldName1": {
"name": "CorrM",
"ip": "127.0.0.1"
},
"FieldName2": {
"bla1": "bla",
"bla2": "bla"
}
}

最新更新