简单json对象for asp

正在做一个asp+jquery的网上答疑系统
今天放在学校的服务器上测试了一下
结果一直有问题,但是什么错都不报
整了半天原来是编码问题,文件是utf-8的编码
改成gb2312就好了
但是asp生成json的时候提示
Microsoft VBScript 运行时错误 错误 '800a01ad' 

ActiveX 部件不能创建对象: 'Scripting.Dictionary' 

服务器又没办法改,只能改代码
幸运的是网上的高手还是多
我找个了简单json对象for asp
改了一下就可以用了
不过需要转换编码



下面是我找的一个简单json对象for asp

< Script language = " VBScript "  runat = " Server " >
' ===========================================================
'
\ <Assembly>
'
\    <Name>Json</Name>
'
\     <Version>1.0.11.6</Version>
'
\     <Classes>
'
\         <Class value="simpleJson">
'
\             <Description>操作简单json对象</Description>
'
\         </Class>
'
\     </Classes>
'
\ </Assembly>
'
===========================================================
     ' json 的 string对象,内部类,用户不必自己操作
    CLASS jsonString
        
PRIVATE  p_str
        
        
PUBLIC   SUB  Class_Initialize()
            p_str
= ""
        
END SUB
    
        
PUBLIC   SUB  Class_Terminate()
            p_str
= NULL
        
END SUB
        
        
PUBLIC   PROPERTY   LET  v(value)
            p_str
= value
        
END PROPERTY
        
        
PUBLIC   PROPERTY   GET  v
            v
= p_str
        
END PROPERTY
        
        
PUBLIC   FUNCTION  parseString()
            
IF   isNull (p_str)  THEN
                parseString
= " null "
            
ELSE
                parseString
= " ' " + p_str + " ' "     
            
END   IF
        
END FUNCTION
        
    
END  CLASS
    
    
' json 的 Number对象,内部类,用户不必自己操作
    CLASS jsonNumber
        
PRIVATE  p_number
        
        
PUBLIC   SUB  Class_Initialize()
            p_number
= NULL
        
END SUB
    
        
PUBLIC   SUB  Class_Terminate()
            p_number
= NULL
        
END SUB
        
        
PUBLIC   PROPERTY   LET  v(value)
            p_number
= value
        
END PROPERTY
        
        
PUBLIC   PROPERTY   GET  v
            v
= p_number
        
END PROPERTY
        
        
PUBLIC   FUNCTION  parseString()
            
IF   isNull (p_number)  THEN
                parseString
= " null "
            
ELSE
                parseString
= Cstr (p_number)
            
END   IF
        
END FUNCTION
    
END  CLASS
    
    
' json 的 value对象,包括自动创建string对象,number对象,true,false,null
    CLASS jsonValue
        
PRIVATE  p_val
        
        
PUBLIC   SUB  Class_Initialize()
            p_val
= NULL
        
END SUB
    
        
PUBLIC   SUB  Class_Terminate()
            p_val
= NULL
        
END SUB
        
        
' 给value对象赋值,参数可以是string类型,number类型,boolean类型和null,除外,将全部转化为string类型
         PUBLIC   PROPERTY   LET  v(value)
            
DIM  jStr,jNum
            
IF   varType (value) = vbBoolean  OR   varType (value) = vbNull  THEN
                p_val
= value
            
ELSEIF   varType (value) = vbString  THEN
                
SET  jStr = NEW  jsonString
                jStr.v
= value
                
SET  p_val = jStr
                
SET  jStr = NOTHING
            
ELSEIF   isNumeric (value)  THEN
                
SET  jNum = NEW  jsonNumber
                jNum.v
= value
                
SET  p_val = jNum
                
SET  jNum = NOTHING
            
ELSE
                
SET  jStr = NEW  jsonString
                jStr.v
= Cstr (value)
                
SET  p_val = jStr
                
SET  jStr = NOTHING
            
END   IF
        
END PROPERTY
        
        
PUBLIC   PROPERTY   GET  v
            
IF   isObject (p_val)  THEN
                
SET  v = p_val
            
ELSE
                v
= p_val    
            
END   IF
        
END PROPERTY
        
        
' 对象以合适的形式转换成字符串表示
         PUBLIC   FUNCTION  parseString()
            
IF   isNull (p_val)  THEN
                parseString
= " null "
            
ELSEIF   varType (p_val) = vbBoolean  THEN
                parseString
= LCase ( Cstr (p_val))
            
ELSE
                parseString
= p_val.parseString()
            
END   IF
        
END FUNCTION
        
    
END  CLASS
    
    
' json 的 array对象
    CLASS jsonArray
        
PRIVATE  p_array,p_length
        
        
PUBLIC   SUB  Class_Initialize()
            p_array
= Array ()
            p_length
= 0
        
END SUB
    
        
PUBLIC   SUB  Class_Terminate()
            p_array
= NULL
            p_length
= 0
        
END SUB
        
        
' 动态设置数组长度
         PUBLIC   PROPERTY   LET  length(value)
            
IF  value < 0   THEN  value = 0
            p_length
= value
            
REDIM  Preserve p_array(p_length)
        
END PROPERTY
        
        
' 获得数组长度
         PUBLIC   PROPERTY   GET  length()
            length
= p_length
        
END PROPERTY
        
        
' 设置索引为index的元素
         PUBLIC   PROPERTY   LET  at(index,ByRef value)
            
IF  index >= 0   OR  index < p_length  THEN
                
SET  p_array(index) = value
            
END   IF
        
END PROPERTY         
        
        
' 获得索引为index的元素,如果index超出范围,返回null
         PUBLIC   PROPERTY   GET  at(index)
            
IF  index < 0   OR  index >= p_length  THEN
                at
= NULL
            
ELSE
                
SET  at = p_array(index)
            
END   IF
        
END PROPERTY
    
        
' 添加value到数组末尾
         PUBLIC   SUB  push(ByRef value)
            p_length
= p_length + 1
            
REDIM  Preserve p_array(p_length)
            
SET  p_array(p_length - 1 ) = value
        
END SUB
        
        
' 获得数组末尾元素,并从数组中删除该元素
         PUBLIC   FUNCTION  pop()
            
SET  pop = p_array(p_length - 1 )
            p_length
= p_length - 1
            
REDIM  Preserve p_array(p_length)
        
END FUNCTION
        
        
' 添加value到数组头部
         PUBLIC   SUB  unshift(ByRef value)
            
DIM  i,jVal
            p_length
= p_length + 1
            
REDIM  Preserve p_array(p_length)
            
FOR  i = p_length - 1   TO   1  STEP  - 1
                
SET  p_array(i) = p_array(i - 1 )
            
NEXT
            
SET  p_array( 0 ) = value
        
END SUB
        
        
' 获得数组头部元素,并从数组中删除该元素
         PUBLIC   FUNCTION  shift()
            
DIM  i
            
SET  shift = p_array( 0 )
            
FOR  i = 0   TO  p_length - 2
                
SET  p_array(i) = p_array(i + 1 )
            
NEXT
            p_length
= p_length - 1
            
REDIM  Preserve p_array(p_length)
        
END FUNCTION
        
        
' 翻转数组
         PUBLIC   SUB  reverse()
            
DIM  i,temp
            
FOR  i = 0   TO   Cint ((p_length - 1 ) / 2 )
                
SET  temp = p_array(i)
                
SET  p_array(i) = p_array(p_length - 1 - i)
                
SET  p_array(p_length - 1 - i) = temp
                
SET  temp = NOTHING
            
NEXT
        
END SUB
        
        
' 对象以合适的形式转换成字符串表示
         PUBLIC   FUNCTION  parseString()
            
DIM  i,str,val
            str
= " [_VALUES] "
            
FOR  i = 0   TO  p_length - 1
                val
= p_array(i).parseString()
                str
= Replace (str, " _VALUES " ,val + " ,_VALUES " )
            
NEXT
            str
= Replace (str, " ,_VALUES " , "" )
            parseString
= str
        
END FUNCTION
    
END  CLASS
    
    
' json 的 object对象
    CLASS jsonObject
        
PRIVATE  p_member,p_value,p_length
    
        
PUBLIC   SUB  Class_Initialize()
            p_member
= Array ()
            p_value
= Array ()
            p_length
= 0
        
END SUB
    
        
PUBLIC   SUB  Class_Terminate()
            p_member
= NULL
            p_value
= NULL
            p_length
= 0
        
END SUB
        
        
' 检查是否包含指定member,返回该member的顺序索引号,没有返回-1
         PRIVATE   FUNCTION  include(member)
            
DIM  i
            
FOR  i = p_length - 1   TO   0  STEP  - 1
                
IF  p_member(i) = member  THEN   EXIT   FOR
            
NEXT
            include
= i
        
END FUNCTION
        
        
' 设置member的value对象
         PUBLIC   PROPERTY   SET  member(mem,ByRef value)
            
DIM  index,length
            index
= include(mem)
            
IF  index =- 1   THEN
                p_length
= p_length + 1
                
REDIM  Preserve p_member(p_length)
                
REDIM  Preserve p_value(p_length)
                p_member(p_length
- 1 ) = mem
                
SET  p_value(p_length - 1 ) = value
            
ELSE
                
SET  p_value(index) = value
            
END   IF
        
END PROPERTY
        
        
' 获得member的value对象
         PUBLIC   PROPERTY   GET  member(mem)
            
DIM  index
            index
= include(mem)
            
IF  index =- 1   THEN
                member
= NULL
            
ELSE
                
SET  member = p_value(index)
            
END   IF
        
END PROPERTY
        
        
' 对象以合适的形式转换成字符串表示
         PUBLIC   FUNCTION  parseString()
            
DIM  str,i,mem,val
            str
= " {_MEMBERS} "
            
FOR  i = 0   TO  p_length - 1
                mem
= p_member(i)
                val
= p_value(i).parseString()
                str
= Replace (str, " _MEMBERS " ,mem + " : " + val + " ,_MEMBERS " )
            
NEXT
            str
= Replace (str, " ,_MEMBERS " , "" )
            parseString
= str
        
END FUNCTION
    
    
END  CLASS
    
    
' 操作简单json for asp的类
    CLASS simpleJson
        
PUBLIC   SUB  Class_Initialize()
        
END SUB
    
        
PUBLIC   SUB  Class_Terminate()
        
END SUB
        
        
' 获得 json的object对象新实例
         PUBLIC   FUNCTION  nO()
            
SET  nO = NEW  jsonObject
        
END FUNCTION
        
        
' 获得 json的array对象新实例
         PUBLIC   FUNCTION  nA()
            
SET  nA = NEW  jsonArray
        
END FUNCTION
        
        
' 获得 json的value对象新实例
         PUBLIC   FUNCTION  nV(value)
            
DIM  jVal
            
SET  jVal = NEW  jsonValue
            jVal.v
= value
            
SET  nV = jVal
        
END FUNCTION
        
        
' 绑定数据数据,转换成简单json对象,第一个参
Asp 这个老古懂估计没几个人在用了。几年没写代码了,最近要弄个小东西,给手机端提供json数据,不想麻烦别人,自己又只会asp,没办法就自己动手了。网上找了好久都没有一个人能完整的把asp操作json说清楚。最后还是自己搞定的。整出来共享给大家。(ps,还有个原因csdn的分不够用啦,大家看着给点吧。写这个说明文档都用了我两小时。^_^) 以下是示例代码 '说明:json.asp中引用了json.js.asp '其他见文档 '手机很多时候不认gb2312,跳入json的坑就忘记gb2312吧,讨厌的是,如果代码报错,iis会输出gb2312,结果就是乱码,有点烦。 '自己想办法解决吧 response.Charset= "utf-8" dim strJsonData,ovbJson,j dim arrTemp,varname ,i set ovbJson=new vbJson 'asp recrodset和数组转json字符 arrTemp=array("a","{""oa"":""我是oa""}","c") strJsonData=ovbjson.toJson(empty,arrTemp,true) '转换为Json格式的字符串,有兴趣可以自己输出看看是什么 set j=json.parse(strJsonData) '序列化为json对象(或者是数组对象) response.Write(j.get(1)&"") '别用vb数组来存json对象,不然得每个元素去重新序列化,这里如果想j.get(1).oa就不行了。必须对j.get(1)单独序列才行 '----recrodset就不演示了,懒得连数据库 '---自定义操作方法的演示--- strJsonData="{a:1,b:[{c:'我是数组中的点c'}]}" set j=json.parsestr(strJsonData) response.Write(j.b.get(0).c&"") '添加节点的时候注意,如果值是null,会被忽然,这个节点会不存在的。在添加之前记得先检查值 set j=json.add(j,"new","我是新加的节点") response.Write(j.new&"") '下面这句注掉了,是因为这个操作是无效的因为j.b是数组,不能add 'set j=json.add(j.b,"new1","我是加不进的节点") set j.b=j.b.put(j.b.length,j.b.get(0)) response.Write(j.b.get(1).c&",我是新加的数组元素") '因为数组的get方法不允许被赋值,所以不能像下面这样写 'set j.b.get(0)=json.add(j.b.get(0),"new","我会报错") json.add j.b.get(0),"new","我是新加的new我不会报错" json.add j.b.get(0),"new1","我是通过变量取出来的哦" response.Write(j.b.get(0).new&"") varname="new1" response.Write(json.byname(j.b.get(0),varname)&"") for i=0 to j.b.length-1 varname="c" response.Write(json.byname(j.b.get(i),varname)&"我是循环出来的c,索引:"&i&" ") next '最后完整的输出给手机就这样: response.Write json.stringify(j)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值