欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-26 08:44:38
首先定义soap数据,然后创建http对象,然后使用post提交,获取状态码为200,就说明调用成功,再进行下一步操作…… 看一下具体实现的代码吧

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

看一下具体实现的代码吧

<!--#include virtual="/include/json.min.asp"-->
<%
dim strxml
dim str

'定义soap消息
strxml = "<?xml version='1.0' encoding='utf-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/'>"       
strxml = strxml & "<soap:body> "
strxml = strxml & "<getcategories xmlns='http://tempuri.org/'>"
strxml = strxml & "<reqcode>30000001</reqcode>" '参数1
strxml = strxml & "<sign>ssssss</sign>" '参数2
strxml = strxml & "</getcategories>"
strxml = strxml & "</soap:body>"
strxml = strxml & "</soap:envelope>"

set h = createobject( "microsoft.xmlhttp")
'向指定的url发送post消息
h.open "post", "http://www.domain.com/webservice.asmx", false
h.setrequestheader "content-type", "text/xml; charset=utf-8"
h.setrequestheader "content-length",len(strxml)
h.setrequestheader "soapaction", "http://tempuri.org/getcategories"
h.send (strxml)

'显示返回的xml信息

if h.status = 200 then
  set xmldoc = server.createobject("msxml.domdocument")
  xmldoc.load(h.responsexml)
  jsontext = xmldoc.childnodes(1).text 
  
  '转换成json,调用json.min.asp
  set categories = json.parse(jsontext)
  
  for i = 0 to categories.length -1
    response.write(cstr(categories.get(i).categoryname)) '属性
    for j = 0 to categories.get(i).labels.length - 1 '集合
      response.write(cstr(categories.get(i).labels.get(j).labelname))
    next
  next
  <pre name="code" class="vb"><script language="javascript" runat="server">
if(!array.prototype.get){array.prototype.get=function(prop){return this[prop];}}"use strict";if(!this.json){json={};}
(function(){function f(n){return n<10?'0'+n:n;}
if(typeof date.prototype.tojson!=='function'){date.prototype.tojson=function(key){return isfinite(this.valueof())?this.getutcfullyear()+'-'+
f(this.getutcmonth()+1)+'-'+
f(this.getutcdate())+'t'+
f(this.getutchours())+':'+
f(this.getutcminutes())+':'+
f(this.getutcseconds())+'z':null;};string.prototype.tojson=number.prototype.tojson=boolean.prototype.tojson=function(key){return this.valueof();};}
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+'"';}
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);}
if(typeof rep==='function'){value=rep.call(holder,key,value);}
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';}
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';}
v=partial.length===0?'[]':gap?'[\n'+gap+
partial.join(',\n'+gap)+'\n'+
mind+']':'['+partial.join(',')+']';gap=mind;return v;}
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);}}}}
v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+
mind+'}':'{'+partial.join(',')+'}';gap=mind;return v;}}
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;}
rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeof replacer.length!=='number')){throw new error('json.stringify');}
return str('',{'':value});};}
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];}}}}
return reviver.call(holder,key,value);}
cx.lastindex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+
('0000'+a.charcodeat(0).tostring(16)).slice(-4);});}
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;}
throw new syntaxerror('json.parse');};}}());
</script>
set xmldoc = nothingelse response.write h.status &" " response.write h.statustextend if%>

<script language="javascript" runat="server">
if(!array.prototype.get){array.prototype.get=function(prop){return this[prop];}}"use strict";if(!this.json){json={};}
(function(){function f(n){return n<10?'0'+n:n;}
if(typeof date.prototype.tojson!=='function'){date.prototype.tojson=function(key){return isfinite(this.valueof())?this.getutcfullyear()+'-'+
f(this.getutcmonth()+1)+'-'+
f(this.getutcdate())+'t'+
f(this.getutchours())+':'+
f(this.getutcminutes())+':'+
f(this.getutcseconds())+'z':null;};string.prototype.tojson=number.prototype.tojson=boolean.prototype.tojson=function(key){return this.valueof();};}
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+'"';}
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);}
if(typeof rep==='function'){value=rep.call(holder,key,value);}
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';}
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';}
v=partial.length===0?'[]':gap?'[\n'+gap+
partial.join(',\n'+gap)+'\n'+
mind+']':'['+partial.join(',')+']';gap=mind;return v;}
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);}}}}
v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+
mind+'}':'{'+partial.join(',')+'}';gap=mind;return v;}}
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;}
rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeof replacer.length!=='number')){throw new error('json.stringify');}
return str('',{'':value});};}
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];}}}}
return reviver.call(holder,key,value);}
cx.lastindex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+
('0000'+a.charcodeat(0).tostring(16)).slice(-4);});}
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;}
throw new syntaxerror('json.parse');};}}());
</script>