WEBAPI返回值读取通用方法

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-16浏览:1

WEBAPI返回值读取通用方法

WebAPI的返回结果通常都是如下格式

返回结果:

{"Result":{"ResponseStatus":{"ErrorCode":"","IsSuccess":"false","Errors":[{"FieldName":"","Message":"","DIndex":0}],"SuccessEntitys":[{"Id":"","Number":"","DIndex":0}],"SuccessMessages":[{"FieldName":"","Message":"","DIndex":0}],"MsgCode":""},"Id":"","Number":"","NeedReturnData":[{}]}}


通用解析方法:

一、解析成功内容

        public static JArray APIResultSuccess(object retobj)
        {
            JObject jObject = JObject.Parse(JsonConvert.SerializeObject(retobj));
            JArray array = new JArray();
            array = jObject.Value<JObject>("Result").Value<JObject>("ResponseStatus").Value<JArray>("SuccessEntitys");
            return array;
        }


二、解析失败内容

        public static string APIResultErrors(object retobj)
        {
            JObject jObject = JObject.Parse(JsonConvert.SerializeObject(retobj));
            JArray array = new JArray();
            array = jObject.Value<JObject>("Result").Value<JObject>("ResponseStatus").Value<JArray>("Errors");
            string strReturn = string.Empty;
            JObject jobRetBody ;

            foreach (var jErr in array)
            {
                jobRetBody = JObject.Parse(jErr.ToString());
                strReturn += jobRetBody["Message"].Value<string>();
            }

            return strReturn;
        }



点赞

WEBAPI返回值读取通用方法

WebAPI的返回结果通常都是如下格式返回结果:{"Result":{"ResponseStatus":{"ErrorCode":"","IsSuccess":"false","Errors":[{"FieldName":"...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息