系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 网络编程 >  ASP编程 > 详细页面

ASP调用WebService转化成JSON数据,附json.min.asp

时间:2020-02-03来源:电脑系统城作者:电脑系统城

首先定义SOAP数据,然后创建HTTP对象,然后使用POST提交,获取状态码为200,就说明调用成功,再进行下一步操作……

看一下具体实现的代码吧


 
  1. <!--#Include virtual="/Include/json.min.asp"-->
  2. <%
  3. Dim strxml
  4. Dim str
  5.  
  6. '定义soap消息
  7. strxml = "<?xml version='1.0' encoding='utf-8'?>"
  8. strxml = strxml & "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
  9. strxml = strxml & "<soap:Body> "
  10. strxml = strxml & "<GetCategories xmlns='http://tempuri.org/'>"
  11. strxml = strxml & "<reqcode>30000001</reqcode>" '参数1
  12. strxml = strxml & "<sign>ssssss</sign>" '参数2
  13. strxml = strxml & "</GetCategories>"
  14. strxml = strxml & "</soap:Body>"
  15. strxml = strxml & "</soap:Envelope>"
  16.  
  17. Set h = createobject( "Microsoft.XMLHTTP")
  18. '向指定的URL发送Post消息
  19. h.open "POST", "http://www.domain.com/WebService.asmx", False
  20. h.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
  21. h.setRequestHeader "Content-Length",LEN(strxml)
  22. h.setRequestHeader "SOAPAction", "http://tempuri.org/GetCategories"
  23. h.send (strxml)
  24.  
  25. '显示返回的XML信息
  26.  
  27. If h.Status = 200 Then
  28. Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
  29. xmlDOC.load(h.responseXML)
  30. jsonText = xmlDOC.childNodes(1).Text
  31.  
  32. '转换成JSON,调用json.min.asp
  33. Set categories = JSON.parse(jsonText)
  34.  
  35. for i = 0 to categories.Length -1
  36.   Response.Write(CStr(categories.get(i).CategoryName)) '属性
  37.   for j = 0 to categories.get(i).Labels.Length - 1 '集合
  38.   Response.Write(CStr(categories.get(i).Labels.get(j).LabelName))
  39.   next
  40. next
  41. <pre name="code" class="vb"><script language="javascript" runat="Server">
  42. if(!Array.prototype.get){Array.prototype.get=function(prop){return this[prop];}}"use strict";if(!this.JSON){JSON={};}
  43. (function(){function f(n){return n<10?'0'+n:n;}
  44. if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){return isFinite(this.valueOf())?this.getUTCFullYear()+'-'+
  45. f(this.getUTCMonth()+1)+'-'+
  46. f(this.getUTCDate())+'T'+
  47. f(this.getUTCHours())+':'+
  48. f(this.getUTCMinutes())+':'+
  49. f(this.getUTCSeconds())+'Z':null;};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf();};}
  50. var cx=/[\?\?\?-\?\?\?\?\?-\?\?-\?\?-\?\?\?-\?]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\?\?-\?\?\?\?\?-\?\?-\?\?-\?\?\?-\?]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';}
  51. function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==='object'&&typeof value.toJSON==='function'){value=value.toJSON(key);}
  52. if(typeof rep==='function'){value=rep.call(holder,key,value);}
  53. switch(typeof value){case'string':return quote(value);case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';}
  54. gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[object Array]'){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||'null';}
  55. v=partial.length===0?'[]':gap?'[\n'+gap+
  56. partial.join(',\n'+gap)+'\n'+
  57. mind+']':'['+partial.join(',')+']';gap=mind;return v;}
  58. if(rep&&typeof rep==='object'){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeof k==='string'){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}
  59. v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+
  60. mind+'}':'{'+partial.join(',')+'}';gap=mind;return v;}}
  61. if(typeof JSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){var i;gap='';indent='';if(typeof space==='number'){for(i=0;i<space;i+=1){indent+=' ';}}else if(typeof space==='string'){indent=space;}
  62. rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeof replacer.length!=='number')){throw new Error('JSON.stringify');}
  63. return str('',{'':value});};}
  64. if(typeof JSON.parse!=='function'){JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==='object'){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{delete value[k];}}}}
  65. return reviver.call(holder,key,value);}
  66. cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+
  67. ('0000'+a.charCodeAt(0).toString(16)).slice(-4);});}
  68. if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');return typeof reviver==='function'?walk({'':j},''):j;}
  69. throw new SyntaxError('JSON.parse');};}}());
  70. </script>
  71. Set xmlDOC = nothingElse Response.Write h.Status &" " Response.Write h.StatusTextEnd if%>

 

附:json.min.asp

 
  1. <script language="javascript" runat="Server">
  2. if(!Array.prototype.get){Array.prototype.get=function(prop){return this[prop];}}"use strict";if(!this.JSON){JSON={};}
  3. (function(){function f(n){return n<10?'0'+n:n;}
  4. if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){returnisFinite(this.valueOf())?this.getUTCFullYear()+'-'+
  5. f(this.getUTCMonth()+1)+'-'+
  6. f(this.getUTCDate())+'T'+
  7. f(this.getUTCHours())+':'+
  8. f(this.getUTCMinutes())+':'+
  9. f(this.getUTCSeconds())+'Z':null;};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf();};}
  10. var cx=/[\?\?\?-\?\?\?\?\?-\?\?-\?\?-\?\?\?-\?]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\?\?-\?\?\?\?\?-\?\?-\?\?-\?\?\?-\?]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';}
  11. function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeofvalue==='object'&&typeof value.toJSON==='function'){value=value.toJSON(key);}
  12. if(typeof rep==='function'){value=rep.call(holder,key,value);}
  13. switch(typeof value){case'string':return quote(value);case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';}
  14. gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[object Array]'){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||'null';}
  15. v=partial.length===0?'[]':gap?'[\n'+gap+
  16. partial.join(',\n'+gap)+'\n'+
  17. mind+']':'['+partial.join(',')+']';gap=mind;return v;}
  18. if(rep&&typeof rep==='object'){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeofk==='string'){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}
  19. v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+
  20. mind+'}':'{'+partial.join(',')+'}';gap=mind;return v;}}
  21. if(typeof JSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){vari;gap='';indent='';if(typeof space==='number'){for(i=0;i<space;i+=1){indent+=' ';}}elseif(typeof space==='string'){indent=space;}
  22. rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeofreplacer.length!=='number')){throw new Error('JSON.stringify');}
  23. return str('',{'':value});};}
  24. if(typeof JSON.parse!=='function'){JSON.parse=function(text,reviver){var j;functionwalk(holder,key){var k,v,value=holder[key];if(value&&typeof value==='object'){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{delete value[k];}}}}
  25. return reviver.call(holder,key,value);}
  26. cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+
  27. ('0000'+a.charCodeAt(0).toString(16)).slice(-4);});}
  28. if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');return typeof reviver==='function'?walk({'':j},''):j;}
  29. throw new SyntaxError('JSON.parse');};}}());
  30. </script>
分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载