ASP调用WebService转化成JSON数据,附json.min.asp
首先定义SOAP数据,然后创建HTTP对象,然后使用POST提交,获取状态码为200,就说明调用成功,再进行下一步操作……
看一下具体实现的代码吧
<!--#Includevirtual="/Include/json.min.asp"-->
<%
Dimstrxml
Dimstr
'定义soap消息
strxml="<?xmlversion='1.0'encoding='utf-8'?>"
strxml=strxml&"<soap:Envelopexmlns: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&"<GetCategoriesxmlns='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>"
Seth=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信息
Ifh.Status=200Then
SetxmlDOC=server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(h.responseXML)
jsonText=xmlDOC.childNodes(1).Text
'转换成JSON,调用json.min.asp
Setcategories=JSON.parse(jsonText)
fori=0tocategories.Length-1
Response.Write(CStr(categories.get(i).CategoryName))'属性
forj=0tocategories.get(i).Labels.Length-1'集合
Response.Write(CStr(categories.get(i).Labels.get(j).LabelName))
next
next
<prename="code"class="vb"><scriptlanguage="javascript"runat="Server">
if(!Array.prototype.get){Array.prototype.get=function(prop){returnthis[prop];}}"usestrict";if(!this.JSON){JSON={};}
(function(){functionf(n){returnn<10?'0'+n:n;}
if(typeofDate.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){returnisFinite(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){returnthis.valueOf();};}
varcx=/[\?\?\?-\?\?\?\?\?-\?\?-\?\?-\?\?\?-\?]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\?\?-\?\?\?\?\?-\?\?-\?\?-\?\?\?-\?]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;functionquote(string){escapable.lastIndex=0;returnescapable.test(string)?'"'+string.replace(escapable,function(a){varc=meta[a];returntypeofc==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';}
functionstr(key,holder){vari,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeofvalue==='object'&&typeofvalue.toJSON==='function'){value=value.toJSON(key);}
if(typeofrep==='function'){value=rep.call(holder,key,value);}
switch(typeofvalue){case'string':returnquote(value);case'number':returnisFinite(value)?String(value):'null';case'boolean':case'null':returnString(value);case'object':if(!value){return'null';}
gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[objectArray]'){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;returnv;}
if(rep&&typeofrep==='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(kinvalue){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;returnv;}}
if(typeofJSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){vari;gap='';indent='';if(typeofspace==='number'){for(i=0;i<space;i+=1){indent+='';}}elseif(typeofspace==='string'){indent=space;}
rep=replacer;if(replacer&&typeofreplacer!=='function'&&(typeofreplacer!=='object'||typeofreplacer.length!=='number')){thrownewError('JSON.stringify');}
returnstr('',{'':value});};}
if(typeofJSON.parse!=='function'){JSON.parse=function(text,reviver){varj;functionwalk(holder,key){vark,v,value=holder[key];if(value&&typeofvalue==='object'){for(kinvalue){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{deletevalue[k];}}}}
returnreviver.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+')');returntypeofreviver==='function'?walk({'':j},''):j;}
thrownewSyntaxError('JSON.parse');};}}());
</script>
SetxmlDOC=nothingElseResponse.Writeh.Status&""Response.Writeh.StatusTextEndif%>
附:json.min.asp
<scriptlanguage="javascript"runat="Server">
if(!Array.prototype.get){Array.prototype.get=function(prop){returnthis[prop];}}"usestrict";if(!this.JSON){JSON={};}
(function(){functionf(n){returnn<10?'0'+n:n;}
if(typeofDate.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){returnisFinite(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){returnthis.valueOf();};}
varcx=/[\?\?\?-\?\?\?\?\?-\?\?-\?\?-\?\?\?-\?]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\?\?-\?\?\?\?\?-\?\?-\?\?-\?\?\?-\?]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;functionquote(string){escapable.lastIndex=0;returnescapable.test(string)?'"'+string.replace(escapable,function(a){varc=meta[a];returntypeofc==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';}
functionstr(key,holder){vari,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeofvalue==='object'&&typeofvalue.toJSON==='function'){value=value.toJSON(key);}
if(typeofrep==='function'){value=rep.call(holder,key,value);}
switch(typeofvalue){case'string':returnquote(value);case'number':returnisFinite(value)?String(value):'null';case'boolean':case'null':returnString(value);case'object':if(!value){return'null';}
gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[objectArray]'){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;returnv;}
if(rep&&typeofrep==='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(kinvalue){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;returnv;}}
if(typeofJSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){vari;gap='';indent='';if(typeofspace==='number'){for(i=0;i<space;i+=1){indent+='';}}elseif(typeofspace==='string'){indent=space;}
rep=replacer;if(replacer&&typeofreplacer!=='function'&&(typeofreplacer!=='object'||typeofreplacer.length!=='number')){thrownewError('JSON.stringify');}
returnstr('',{'':value});};}
if(typeofJSON.parse!=='function'){JSON.parse=function(text,reviver){varj;functionwalk(holder,key){vark,v,value=holder[key];if(value&&typeofvalue==='object'){for(kinvalue){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{deletevalue[k];}}}}
returnreviver.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+')');returntypeofreviver==='function'?walk({'':j},''):j;}
thrownewSyntaxError('JSON.parse');};}}());
</script>