背景:

  近期帮一个公司做第三方API的二次封装,需要部署到该公司网站。所获取的是Json数据格式。由于该公司原系统采用的ASP+VBS技术方案,因此采用VBS对API进行请求、封装。

实现:

废话不多说,直接上代码。由于某些原因,不能将所有代码贴出来,有问题的可以留言。

<!--#include file="LibJson.asp"-->

<script Language="jscript" runat="server">
Array.prototype.get = function(x) { return this[x]; };
function parseJSON(strJSON) { return eval("(" + strJSON + ")"); }
</script> <% Sub writeData()
Set My = MoLibJson.new() My.dateformat = "yyyy-MM-dd HH:mm:ss"
My.put "status","ok"
My.put "time",Now Set results = My.put("result")
results.datatype="array" Set writetoResult = Server.CreateObject("Scripting.FileSystemObject") writetoResult.CreateTextFile ResultPh,true Set resufile = writetoResult.OpenTextFile(ResultPh,,True)
resufile.Write My.toString()
Set resufile = Nothing End Sub Sub getDataByLocation (longitude,latitude)
URL = “http:xxxx.com/xx.json”
weatherData = GetHttpPage(URL,"UTF-8") 'Http请求,获取返回的json数据
Set obj = parseJSON(weatherData) ‘对获取到的json数据进行解析 ‘生成json数据
Set result1 = results.put()
result1.put "location",array(longitude,latitude),"vbarray" Set subPre = result1.put("precipitation")
Set preHou = subPre.put("hourly")
preHou.datatype="array"
Set hou= obj.result.hourly.precipitation
For n= To
Set kvHou = preHou.put()
kvHou.put "value",hou.get(n).value
kvHou.put "datetime",hou.get(n).datetime
Next Dim arrayData()
Set minu= obj.result.minutely.precipitation
For n= To
arrayData(n)= minu.get(n)
Next
subPre.put "minutely",arrayData,"vbarray" End Sub Function GetHttpPage(url, charset)
Dim http
Set http = Server.createobject("Msxml2.ServerXMLHTTP")
'Set http = Server.createobject("MSXML2.XMLHTTP.6.0")
'set http = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
http.setOption() =
http.Open "GET", url, false
http.Send()
If http.readystate<> Then
Exit Function
End If
GetHttpPage = BytesToStr(http.ResponseBody, charset)
Set http = Nothing
End function Function BytesToStr(body, charset)
Dim objStream
Set objStream = Server.CreateObject("Adodb.Stream")
objStream.Type =
objStream.Mode =
objStream.Open
objStream.Write body
objStream.Position =
objStream.Type =
objStream.Charset = charset
BytesToStr = objStream.ReadText
objStream.Close
Set objStream = Nothing
End Function %>

LibJson.asp.

这个代码是用的别人的,可以直接用

<script language="jscript" runat="server">
/*'by anlige at www.9fn.net*/
function MoLibJson(t,df){
this.table=[];
this.datatype=t||"object";
this.dateformat=df||"";
}
MoLibJson.New = function(t,df){return new MoLibJson(t,df);};
MoLibJson.prototype.put=function(key,value,tp){
if(value===undefined)return this.putnew(key||"TMP__");
tp = tp||"";
tp = tp.toLowerCase();
if(tp=="vbarray" || this.IsVBArray(value))return this.putvbarray(key,value);
if(tp=="arraylist")return this.put(key,value.split(","));
if(tp=="dictionary" || this.IsDictionary(value))return this.putdictionary(key,value);
if(tp=="rows" || this.IsRecordset(value))return this.putrows(key,value);
this.table.push({key:key,value:value});
return this.table[this.table.length-];a
};
MoLibJson.prototype.putdictionary=function(key,value){
return this.putnew(key).fromdictionary(value);
};
MoLibJson.prototype.fromdictionary=function(value){
if(value.count<=)return obj;
var keys=(new VBArray(value.keys())).toArray();
for(var i =;i<keys.length;i++){
this.put(keys[i],value.item(keys[i]));
}
return this;
};
MoLibJson.prototype.putnew=function(key){
return this.put(key,new MoLibJson("object",this.dateformat))['value'];
};
MoLibJson.prototype.putnewarray=function(key){
return this.put(key,new MoLibJson("array",this.dateformat))['value'];
};
MoLibJson.prototype.putvbarray=function(key,value){
return this.put(key,(new VBArray(value)).toArray());
};
MoLibJson.prototype.putrows=function(key,rs,pagesize){
return this.putnewarray(key).fromrows(rs,pagesize);
};
MoLibJson.prototype.fromrows=function(rs,pagesize){
this.datatype="array";
if(pagesize==undefined)pagesize=-;
var ps = rs.Bookmark;
var k=;
while(!rs.eof && (k<pagesize || pagesize==-)){
k++;
var tmp__=new Object();
for(var i=;i<rs.fields.count;i++){
tmp__[rs.fields(i).Name]=rs.fields(i).value;
}
this.put("",tmp__);
rs.MoveNext();
}
try{
rs.Bookmark=ps;
}catch(ex){}
return this;
};
MoLibJson.prototype.toString = MoLibJson.prototype.getobjectstring=function(key){
key=key||"";
if(this.datatype=="array"){
if(this.table.length<=)return "[]";
var ret="[";
for(var i=;i<this.table.length;i++){
ret+=this.parse(this.table[i]["value"]) + ",";
}
if(ret!="[")ret=ret.substr(,ret.length-);
ret+="]";
if(key!="")return "{\"" + key + "\":" + ret + "}";
return ret;
}else{
if(this.table.length<=)return "{}";
var ret="{";
for(var i=;i<this.table.length;i++){
ret+="\""+this.table[i]["key"]+"\":" + this.parse(this.table[i]["value"]) + ",";
}
if(ret!="{")ret=ret.substr(,ret.length-);
ret+="}";
if(key!="")return "{\"" + key + "\":" + ret + "}";
return ret;
}
};
MoLibJson.prototype.del=function(key){
if(this.table.length<=)return;
var tb = [],find=false;
for(var i=;i<this.table.length;i++){
if(this.table[i]["key"]!=key){
tb.push(this.table[i]);
}else{
find = true;
}
}
if(find)this.table = tb;
};
MoLibJson.prototype.set=function(key,value){
this.del(key);
this.put(key,value);
};
MoLibJson.prototype.parse=function(value){
if(value===null)return "null";
if(this.IsVBArray(value))value=(new VBArray(value)).toArray();
var ty = typeof(value);
if(ty=="string"){
return "\"" + this.encode(value) + "\"";
}else if(ty=="number" || ty=="boolean"){
return value;
}else if(ty=="date"){
var dt = new Date(value);
if(this.dateformat==""){
return "\""+dt.toString()+"\"";
}else{
return "\""+this.formatdate(value,this.dateformat)+"\"";
}
}else if(ty=="object"){
if(value.constructor){
if(value.constructor==Array){
var ret1="[";
for(var j=;j<value.length;j++){
ret1+=this.parse(value[j])+",";
}
if(ret1!="[")ret1=ret1.substr(,ret1.length-);
ret1+="]";
return ret1;
}else if(value.constructor==MoLibJson){
return value.getobjectstring();
}else if(value.constructor==Object){
var o = new MoLibJson("object",this.dateformat);
for(var i in value){
if(value.hasOwnProperty && value.hasOwnProperty(i)) o.put(i,value[i]);
}
return o.getobjectstring();
}else if(value.constructor==Date){
var dt = new Date(value);
if(this.dateformat==""){
return "\""+dt.toString()+"\"";
}else{
return "\""+this.formatdate(value,this.dateformat)+"\"";
}
}else{
return this.encode(value.constructor.toString());
}
}
}
return "\"unknown:" + ty +"\"";
};
MoLibJson.prototype.formatstring=function(src,format){
if(!/^(left|right)\s(\d+)$/i.test(format))return src;
var direction = /^(left|right)\s(\d+)$/i.exec(format)[];
var len = parseInt(/^(left|right)\s(\d+)$/i.exec(format)[]);
if(direction=="left")return src.substr(,len);
if(direction=="right")return src.substr(src.length-len,len);
};
MoLibJson.prototype.formatdate=function(dt,formatStr){
if(typeof dt!="date")dt=new Date();
dt = new Date(dt);
var y=new Array(),m=new Array(),d=new Array(),h=new Array(),n=new Array(),s=new Array(),w,ws=new Array(),t = new Array(),H=new Array();
y[] = dt.getFullYear();
m[] = dt.getMonth()+;
d[] = dt.getDate();
h[] = dt.getHours();
H[] = h[] % ;
n[] = dt.getMinutes();
s[] = dt.getSeconds();
y[] = y[];
m[] = this.formatstring("" +m[],"right 2");
d[] = this.formatstring("" +d[],"right 2");
h[] = this.formatstring("" +h[],"right 2");
H[] = this.formatstring("" +H[],"right 2");
n[] = this.formatstring("" +n[],"right 2");
s[] = this.formatstring("" +s[],"right 2");
ws[] = "日,一,二,三,四,五,六".split(",");
ws[] = "Sun,Mon,Tues,Wed,Thur,Fri,Sat".split(",");
t[] = dt.getMilliseconds();
w=dt.getDay()-; var returnStr;
returnStr = formatStr.replace(/yyyy/g,y[]);
returnStr = returnStr.replace(/yy/g,y[]);
returnStr = returnStr.replace(/MM/g,m[]);
returnStr = returnStr.replace(/M/g,m[]);
returnStr = returnStr.replace(/dddd/g,ws[][w]);
returnStr = returnStr.replace(/ddd/g,ws[][w]);
returnStr = returnStr.replace(/dd/g,d[]);
returnStr = returnStr.replace(/d/g,d[]);
returnStr = returnStr.replace(/HH/g,h[]);
returnStr = returnStr.replace(/H/g,h[]);
returnStr = returnStr.replace(/mm/g,n[]);
returnStr = returnStr.replace(/m/g,n[]);
returnStr = returnStr.replace(/ss/g,s[]);
returnStr = returnStr.replace(/s/g,s[]);
returnStr = returnStr.replace(/tttt/g,t[]);
return returnStr ;
};
MoLibJson.prototype.encode=function(str){
if(str==undefined) return "";
if(str=="")return "";
var i, j, aL1, aL2, c, p,ret="";
aL1 = Array(0x22, 0x5C, 0x2F, 0x08, 0x0C, 0x0A, 0x0D, 0x09);
aL2 = Array(0x22, 0x5C, 0x2F, 0x62, 0x66, 0x6E, 0x72, 0x74);
for(i = ;i<str.length;i++){
p = true;
c = str.substr(i,);
for(j = ;j<=;j++){
if(c == String.fromCharCode(aL1[j])){
ret += "\\" + String.fromCharCode(aL2[j]);
p = false;
break;
}
}
if(p){
var a = c.charCodeAt();
if(a > && a < ){
ret +=c
}else if(a > - || a < ){
var slashu = a.toString();
while(slashu.length<){slashu=""+slashu;}
ret += "\\u" + slashu;
}
}
}
return ret;
};
MoLibJson.prototype.IsDictionary=function(o) {
return ((o != null) &&
(typeof(o) == "object") &&
(o instanceof ActiveXObject) &&
(typeof(o.Add) == "unknown") &&
(typeof(o.Exists) == "unknown") &&
(typeof(o.Items) == "unknown") &&
(typeof(o.Keys) == "unknown") &&
(typeof(o.Remove) == "unknown") &&
(typeof(o.RemoveAll) == "unknown") &&
(typeof(o.Count) == "number") &&
(typeof(o.Item) == "unknown") &&
(typeof(o.Key) == "unknown"));
}; MoLibJson.prototype.IsVBArray=function(o) {
return ((o != null) &&
(typeof(o) == "unknown") &&
(o.constructor == VBArray) &&
(typeof(o.dimensions) == "function") &&
(typeof(o.getItem) == "function") &&
(typeof(o.lbound) == "function") &&
(typeof(o.toArray) == "function") &&
(typeof(o.ubound) == "function"));
}
MoLibJson.prototype.IsRecordset=function(o) {
return ((o != null) &&
(typeof(o) == "object") &&
(o instanceof ActiveXObject) &&
(typeof(o.Fields) == "object") &&
(typeof(o.CursorType) == "number") &&
(typeof(o.LockType) == "number") &&
(typeof(o.BookMark) == "number") &&
(typeof(o.AbsolutePage) == "number" || typeof(o.AbsolutePage) == "unknown") &&
(typeof(o.Recordcount) == "number" || typeof(o.Recordcount) == "unknown") &&
(typeof(o.PageSize) == "number"));
}
</script>

VBS进行http请求及JSON数据的读取和生成的更多相关文章

  1. PHP/Post 提交请求获取json数据,并转化为所需要的数组

    /** * Post 提交请求获取json数据,并转化为所需要的数组 */ function request_post($url = '', $param = '') { if (empty($url ...

  2. 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回

    作者:ssslinppp      时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...

  3. 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法

    在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法 最近在做一个小东西,使用kindeditor上传图片的时候,自己写了一个上传的方法,按照协议规则通过ajax返回json ...

  4. 【VueJS】VueJS开发请求本地json数据的配置

    VueJS开发请求本地json数据的配置,旧版本是build/dev-server.js,新版本是build/webpack.dev.conf.js. VueJS开发请求本地json数据的配置,早期的 ...

  5. Nginx下HTML页面POST请求静态JSON数据返回405状态

    在浏览器访问HTML页面,发现一些静态JSON数据没有显示,F12查看,如下图所示: 可以看到请求方式为POST 将请求链接复制在浏览器地址栏访问,可以正常请求到数据 F12查看,可以看到请求方式为G ...

  6. Beego框架POST请求接收JSON数据

    原文: https://blog.csdn.net/Aaron_80726/article/details/83870563 ------------------------------------- ...

  7. 手把手教你vue配置请求本地json数据

    本篇文章主要介绍了vue配置请求本地json数据的方法,分享给大家,具体如下:在build文件夹下找到webpack.dev.conf.js文件,在const portfinder = require ...

  8. Android JSON数据的读取和创建

    预先准备好的一段JSON数据 { "languages":[ {"id":1,"ide":"Eclipse"," ...

  9. Java读取json文件并对json数据进行读取、添加、删除与修改操作

    转载:http://blog.csdn.net/qing_yun/article/details/46865863#t0   1.介绍 开发过程中经常会遇到json数据的处理,而单独对json数据进行 ...

随机推荐

  1. 基于Ionic2的开源项目

    项目介绍 基于Ionic2的Ionic中文论坛客户端,该应用也是边学边做的,为了将更多常用东西加入到APP中,有些逻辑不通之处,敬请包涵. 开源地址 https://github.com/zxj963 ...

  2. 图-最短路径-Dijktra(迪杰斯特拉)算法

    1. 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉算法于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以起始 ...

  3. [java] StringBuilder / StringBuffer / String 建立字符串

    1.三者在建立新字符串速度方面的比较: StringBuilder >  StringBuffer  >  String 2.线程安全性: StringBuilder:线程非安全的 Str ...

  4. 第 29 章 CSS3 弹性伸缩布局[上]

    学习要点: 1.布局简介 2.旧版本 主讲教师:李炎恢 本章主要探讨 HTML5 中 CSS3 提供的用来实现未来响应式弹性伸缩布局方案,这里做一个初步的了解. 一.布局简介 CSS3 提供一种崭新的 ...

  5. Scalaz(1)- 基础篇:隐式转换解析策略-Implicit resolution

    在正式进入scalaz讨论前我们需要理顺一些基础的scalaz结构组成概念和技巧.scalaz是由即兴多态(ad-hoc polymorphism)类型(typeclass)组成.scalaz typ ...

  6. hdu-1856-More is better

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  7. CSDN数据库被爆 统计CSDN用户都喜欢哪些密码

    今天有黑客在网上公开了知名网站CSDN的用户数据库,这是一次严重的暴库泄密事件,涉及到的账户总量高达600万个.有人写了一个小程序,统计了这次公布的 6428632 个 CSDN 哪些密码出镜率较高? ...

  8. Tab切换类型

    Tab切换类型 点击Tab 滑过Tab 延迟Tab CSS样式 ; ; list-style:none; font-size:12px;} .notice{width:298px; height:98 ...

  9. CSS常用标签

    CSS常用标签 一 CSS文字属性 color : #999999; /*文字颜色*/ font-family : 宋体,sans-serif; /*文字字体*/ font-size : 9pt; / ...

  10. [js开源组件开发]loading加载效果

    loading加载效果 由于程序和网络的原因,常常我们需要在交互的时候,给用户一个正在加载中的动画,于是,loading组件横空出世.不需要复杂的代码,也能完成大多数业务,这就是我做组件的原则. 效果 ...