1. 程式人生 > >WebAPI從Post的Body中解析出JObject格式的Request引數

WebAPI從Post的Body中解析出JObject格式的Request引數

方法如下:

        /// <summary>
        /// 解析引數(Post請求Body中的輸入)
        /// </summary>
        /// <param name="s">輸入的檔案流</param>
        /// <returns>解析所得JObject物件</returns>
        public static JObject Parse(Stream s)
        {
            //s = HttpContext.Current.Request.InputStream;
            JObject jObject = new JObject();
            try
            {
                using (var sr = new StreamReader(s, Encoding.UTF8))
                using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(sr))
                {
                    jObject = (new Newtonsoft.Json.JsonSerializer()).Deserialize(jsonTextReader) as JObject;
                }
            }
            catch (Exception e)
            {
                Log.Loging.Error(ResponseManager.FailureWithInvalidParameters());
            }
            return jObject;
        }