将多个对象作为单个对象传递给json请求 [英] pass multiple objects to json request as a single object

查看:73
本文介绍了将多个对象作为单个对象传递给json请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static async Task< string> CallPostApi(字符串路径,字符串_sessionId)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; UserRequest uu = new UserRequest();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;可用性 aa =新可用性();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;名称 nn =新的TestClient.Name();
$


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; uu.extId =" 4410184";

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; aa.available =" Yes";

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; nn.given =" vish";



$
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var client = new RestClient(path);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var request = new RestRequest(Method.POST);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; request.AddHeader(" Postman-Token",_sessionId);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; request.AddHeader(" Cache-Control"," no-cache");

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; request.AddHeader("授权","承载"+ _ sessionId);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; request.AddHeader("Content-Type","application / json");

public static async Task<string> CallPostApi(string path, string _sessionId)
        {
            UserRequest uu = new UserRequest();
            Availability aa = new Availability();
            Name nn = new TestClient.Name();

            uu.extId = "4410184";
            aa.available = "Yes";
            nn.given = "vish";


            var client = new RestClient(path);
            var request = new RestRequest(Method.POST);
            request.AddHeader("Postman-Token", _sessionId);
            request.AddHeader("Cache-Control", "no-cache");
            request.AddHeader("Authorization", "Bearer "+_sessionId);
            request.AddHeader("Content-Type", "application/json");

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var json = JsonConvert.SerializeObject (uu);   在这里,我只传递一个我想传递的对象"aa"和"nn"也请帮我如何
map" aa"和"nn"到"uu"。

            var json = JsonConvert.SerializeObject(uu);   here i am passing only one object i want to pass "aa" and "nn" also please help me how to map "aa" and "nn" to "uu".

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; request.AddParameter(" undefined",
json ,ParameterType.RequestBody);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; IRestResponse response = client.Execute(request);  &NBSP; &NBSP; &NBSP;  

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return response.ToString();

  &NBSP; &NBSP; &NBSP; }

            request.AddParameter("undefined", json, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);         
            return response.ToString();
        }

问候,vishal

推荐答案

这完全取决于远程端点。您几乎无法控制所发送数据的布局,只能控制格式(JSON,XML等)。您需要查看API的文档,它们将准确地告诉您如何发送数据。如果他们
支持多部分表格,那么你将以这种方式发送。如果它们需要JSON对象,那么您必须以您需要的格式存储数据。如果他们支持"多个对象",然后他们会告诉你如何指定它。如果他们不是
那么你就不能这样称呼它。
That is completely up to the remote endpoint. You have little control over the layout of the data you send, just the format (JSON, XML, etc). You need to look at the API's documentation and they will tell you exactly how the data needs to be sent. If they support multi-part forms then you'll send it that way. If they require a JSON object then you'll have to put your data in the format they require. If they support "multiple objects" then they will tell you how it has to be specified. If they don't then you cannot call it that way.


这篇关于将多个对象作为单个对象传递给json请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆