如何在 RestSharp 中 POST 多个不同的字段名称?

How to POST many different field names in RestSharp?

如何在 RestSharp 中 POST 多个不同的字段名称? 下面是我试过的代码,它会像这样还是其他方式。 如有任何帮助,我们将不胜感激。

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这样的数据??

我想要这种格式的 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);

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

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

输出字符串必须像那样

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