Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正文中包含C#.NET复杂json数据的REST api_C#_Json_Rest_Restsharp - Fatal编程技术网

正文中包含C#.NET复杂json数据的REST api

正文中包含C#.NET复杂json数据的REST api,c#,json,rest,restsharp,C#,Json,Rest,Restsharp,我想在我的应用程序中使用sms网关。这就是为什么我联系了一个操作员,操作员给了我一个api格式 网址: 请求头 Content-Type: application/json Authorization: Bearer [access token] Accept: application/json 身体 现在,我已经编写了代码: RestClient client = new RestClient(@"https://ideabiz.lk/"); RestRequest req = new

我想在我的应用程序中使用sms网关。这就是为什么我联系了一个操作员,操作员给了我一个api格式

网址:

请求头

Content-Type: application/json 
Authorization: Bearer [access token] 
Accept: application/json
身体


现在,我已经编写了代码:

RestClient client = new RestClient(@"https://ideabiz.lk/");
RestRequest req = new RestRequest(@"apicall/smsmessaging/v2/outbound/3313/requests", Method.POST);

req.AddHeader("Content-Type", @"application/json");
req.AddHeader("Authorization", @"Bearer " + accessToken.ToString());
req.AddHeader("Accept", @"application/json");

string jSon_Data = @"{'outboundSMSMessageRequest': {'address': ['tel:+94768769027'],'senderAddress': 'tel:3313','outboundSMSTextMessage': {'message': 'Test Message : " + System.DateTime.Now.ToString() + "'},'clientCorrelator': '123456','receiptRequest': {'notifyURL': 'http://128.199.174.220:1080/sms/report','callbackData': 'some-data-useful-to-the-requester'},'senderName': ''}}";


JObject json = JObject.Parse(jSon_Data);
req.AddBody(json);

IRestResponse response = client.Execute(req);
string x = response.Content.ToString();
Console.WriteLine(x.ToString());

当我执行这个程序时

请求AddBody(json)

我的系统崩溃并给出错误消息:

System.Windows.Forms.dll中发生类型为“System.StackOverflowException”的未处理异常


如何使用C#.NET发布复杂的JSON?

这里有两个问题:

  • 在调用
    AddBody
    之前,需要设置
    RequestFormat=DataFormat.Json

        req.RequestFormat = DataFormat.Json;
        req.AddBody(json);
    
    在不设置参数的情况下,RestSharp尝试将
    JObject
    序列化为XML,并在某个地方陷入无限递归——很可能是尝试序列化

  • 最新版本的RestSharp作为其JSON序列化程序:

    There is one breaking change: the default Json*Serializer* is no longer 
    compatible with Json.NET. To use Json.NET for serialization, copy the code 
    from https://github.com/restsharp/RestSharp/blob/86b31f9adf049d7fb821de8279154f41a17b36f7/RestSharp/Serializers/JsonSerializer.cs 
    and register it with your client:
    
    var client = new RestClient();
    client.JsonSerializer = new YourCustomSerializer();
    
    RestSharp的新内置JSON序列化程序不理解
    JObject
    ,因此,如果您使用的是这些最新版本之一,则需要按照上述说明创建:

    public class JsonDotNetSerializer : ISerializer
    {
        private readonly Newtonsoft.Json.JsonSerializer _serializer;
    
        /// <summary>
        /// Default serializer
        /// </summary>
        public JsonDotNetSerializer() {
            ContentType = "application/json";
            _serializer = new Newtonsoft.Json.JsonSerializer {
                MissingMemberHandling = MissingMemberHandling.Ignore,
                NullValueHandling = NullValueHandling.Include,
                DefaultValueHandling = DefaultValueHandling.Include
            };
        }
    
        /// <summary>
        /// Default serializer with overload for allowing custom Json.NET settings
        /// </summary>
        public JsonDotNetSerializer(Newtonsoft.Json.JsonSerializer serializer){
            ContentType = "application/json";
            _serializer = serializer;
        }
    
        /// <summary>
        /// Serialize the object as JSON
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <returns>JSON as String</returns>
        public string Serialize(object obj) {
            using (var stringWriter = new StringWriter()) {
                using (var jsonTextWriter = new JsonTextWriter(stringWriter)) {
                    jsonTextWriter.Formatting = Formatting.Indented;
                    jsonTextWriter.QuoteChar = '"';
    
                    _serializer.Serialize(jsonTextWriter, obj);
    
                    var result = stringWriter.ToString();
                    return result;
                }
            }
        }
    
        /// <summary>
        /// Unused for JSON Serialization
        /// </summary>
        public string DateFormat { get; set; }
        /// <summary>
        /// Unused for JSON Serialization
        /// </summary>
        public string RootElement { get; set; }
        /// <summary>
        /// Unused for JSON Serialization
        /// </summary>
        public string Namespace { get; set; }
        /// <summary>
        /// Content type for serialized content
        /// </summary>
        public string ContentType { get; set; }
    }
    

  • 遵循此说明后,它就可以正常工作了。谢谢@dbc。
    public class JsonDotNetSerializer : ISerializer
    {
        private readonly Newtonsoft.Json.JsonSerializer _serializer;
    
        /// <summary>
        /// Default serializer
        /// </summary>
        public JsonDotNetSerializer() {
            ContentType = "application/json";
            _serializer = new Newtonsoft.Json.JsonSerializer {
                MissingMemberHandling = MissingMemberHandling.Ignore,
                NullValueHandling = NullValueHandling.Include,
                DefaultValueHandling = DefaultValueHandling.Include
            };
        }
    
        /// <summary>
        /// Default serializer with overload for allowing custom Json.NET settings
        /// </summary>
        public JsonDotNetSerializer(Newtonsoft.Json.JsonSerializer serializer){
            ContentType = "application/json";
            _serializer = serializer;
        }
    
        /// <summary>
        /// Serialize the object as JSON
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <returns>JSON as String</returns>
        public string Serialize(object obj) {
            using (var stringWriter = new StringWriter()) {
                using (var jsonTextWriter = new JsonTextWriter(stringWriter)) {
                    jsonTextWriter.Formatting = Formatting.Indented;
                    jsonTextWriter.QuoteChar = '"';
    
                    _serializer.Serialize(jsonTextWriter, obj);
    
                    var result = stringWriter.ToString();
                    return result;
                }
            }
        }
    
        /// <summary>
        /// Unused for JSON Serialization
        /// </summary>
        public string DateFormat { get; set; }
        /// <summary>
        /// Unused for JSON Serialization
        /// </summary>
        public string RootElement { get; set; }
        /// <summary>
        /// Unused for JSON Serialization
        /// </summary>
        public string Namespace { get; set; }
        /// <summary>
        /// Content type for serialized content
        /// </summary>
        public string ContentType { get; set; }
    }
    
        RestRequest req = new RestRequest(@"apicall/smsmessaging/v2/outbound/3313/requests", Method.POST);
        req.JsonSerializer = new JsonDotNetSerializer();