C#:使用RestSharp将JSON对象转换为字符串:
以下是我使用python请求编写的HTTP请求的工作示例。在上述示例中,有一个JSON对象,我使用JSON转储函数将其转换为字符串:
data = [
{"parameterName":"date",
"values":["-30d"]
},
{"parameterName":"category",
"values":["25","4","2"]
},
{"parameterName":"format",
"values":["pdf"]
},
{"parameterName":"readunread","values":["read"]}]
request = session.post(
someUrl,
headers={
'Host': someUrl,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
},
cookies=response.cookies,
data = json.dumps(data)
)
我想找到一种不必要的复杂方法,因为Python太简单了,而想到的第一件事是C#(使用RestSharp(使用RestSharp) ):
var client = new RestSharp.RestClient();
client.BaseUrl = new Uri(someUrl);
var documents = new RestSharp.RestRequest(RestSharp.Method.POST);
documents.AddCookie("cookie",theCookies);
documents.AddHeader("Host",someHostUrl);
documents.AddHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0");
documents.AddHeader("Accept","application/json, text/plain, */*");
documents.AddHeader("Accept-Language", "en-US,en;q=0.5");
documents.AddHeader("Accept-Encoding", "gzip, deflate, br");
string documentsBody = "[{\"parameterName\":\"category\",\"values\":[\"25\",\"4\",\"2\"]},{\"parameterName\":\"format\",\"values\":[\"pdf\"]},{\"parameterName\":\"date\",\"values\":[\"-30d\"]},{\"parameterName\":\"readunread\",\"values\":[\"read\"]}]";
documents.AddBody(documentsBody);
IRestResponse response = client.Execute(documents);
Python示例返回所需的响应。 C#示例没有。我知道这是由于有效载荷的格式。格式化有效载荷(如python示例中所做的那样),但在C#中,最令人讨厌的方法是什么?
Below is a working example of an http request I wrote using Python requests. In said example there is a json object I convert to string using the Json Dumps function:
data = [
{"parameterName":"date",
"values":["-30d"]
},
{"parameterName":"category",
"values":["25","4","2"]
},
{"parameterName":"format",
"values":["pdf"]
},
{"parameterName":"readunread","values":["read"]}]
request = session.post(
someUrl,
headers={
'Host': someUrl,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
},
cookies=response.cookies,
data = json.dumps(data)
)
I wanted to find a unnecessarily complicated way to do this another way since Python is too straight forward and the first thing that came to mind was C# (using RestSharp):
var client = new RestSharp.RestClient();
client.BaseUrl = new Uri(someUrl);
var documents = new RestSharp.RestRequest(RestSharp.Method.POST);
documents.AddCookie("cookie",theCookies);
documents.AddHeader("Host",someHostUrl);
documents.AddHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0");
documents.AddHeader("Accept","application/json, text/plain, */*");
documents.AddHeader("Accept-Language", "en-US,en;q=0.5");
documents.AddHeader("Accept-Encoding", "gzip, deflate, br");
string documentsBody = "[{\"parameterName\":\"category\",\"values\":[\"25\",\"4\",\"2\"]},{\"parameterName\":\"format\",\"values\":[\"pdf\"]},{\"parameterName\":\"date\",\"values\":[\"-30d\"]},{\"parameterName\":\"readunread\",\"values\":[\"read\"]}]";
documents.AddBody(documentsBody);
IRestResponse response = client.Execute(documents);
The Python example returns the desired response. The C# example does not. I know it is due to the formatting of the payload. What is the least obnoxious way to format the payload (as done in the Python example) but in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以尝试使用
>来自
我们可以创建一个代表您的参数数据作为强类型的类。
然后将其作为
jsonconvert.serializeobject
编辑
中的参数传递为参数,如果您不想创建模型参数类,我们可以尝试使用匿名对象。
c#在线
We can try to use
JsonConvert.SerializeObject
which support fromJson.NET
that help us easy to serializeObject or deserialize.We can create a class that represent to carry your parameter data as a strong type.
then passing it as parameter in
JsonConvert.SerializeObject
EDIT
if you don't want to create a class for parameter of model, we can try to use the anonymous object.
c# online