syhzwq 2021-06-23 15:54 采纳率: 50%
浏览 38
已采纳

请教一个关于ASP转JSON的问题

以下是ASP转JSON的类文件代码

<%
class JSON

    private output, innerCall
    
    public toResponse      
    
    public sub class_initialize()
        newGeneration()
        toResponse = false
    end sub
    
    PUBLIC FUNCTION escape(val)
        dim cDoubleQuote, cRevSolidus, cSolidus
            cDoubleQuote = &h22
            cRevSolidus = &h5C
            cSolidus = &h2F
        
        dim i, currentDigit
        for i = 1 to (len(val))
            currentDigit = mid(val, i, 1)
            if ascw(currentDigit) > &h00 and ascw(currentDigit) < &h1F then
                currentDigit = escapequence(currentDigit)
            elseif ascw(currentDigit) >= &hC280 and ascw(currentDigit) <= &hC2BF then
                currentDigit = "\u00" + right(padLeft(hex(asc(currentDigit) - &hC200), 2, 0), 2)
            elseif ascw(currentDigit) >= &hC380 and ascw(currentDigit) <= &hC3BF then
                currentDigit = "\u00" + right(padLeft(hex(ascw(currentDigit) - &hC2C0), 2, 0), 2)
            else
                select case ascw(currentDigit)
                case cDoubleQuote: currentDigit = escapequence(currentDigit)
                case cRevSolidus: currentDigit = escapequence(currentDigit)
                case cSolidus: currentDigit = escapequence(currentDigit)
                end select
            end if
            escape = escape & currentDigit
        next
    END FUNCTION
    
    PUBLIC FUNCTION toJSON(name, val, nested)
        if not nested and not isEmpty(name) then write("{")
        if not isEmpty(name) then write("""" & escape(name) & """: ")
            generateValue(val)
        if not nested and not isEmpty(name) then write("}")
            toJSON = output
        if innerCall = 0 then newGeneration()
    END FUNCTION
    
    PRIVATE FUNCTION generateValue(val)
        if isNull(val) then
            write("null")
        elseif isArray(val) then
            generateArray(val)
        elseif isObject(val) then
            if val is nothing then
                write("null")
            elseif typename(val) = "Dictionary" then
                generateDictionary(val)
            elseif typename(val) = "Recordset" then
                generateRecordset(val)
            else
                generateObject(val)
            end if
        else
            varTyp = varType(val)
            if varTyp = 11 then
            if val then write("true") else write("false")
            elseif varTyp = 2 or varTyp = 3 or varTyp = 17 or varTyp = 19 then
                write(cLng(val))
            elseif varTyp = 4 or varTyp = 5 or varTyp = 6 or varTyp = 14 then
                write(replace(cDbl(val), ",", "."))
            else
                write("""" & escape(val & "") & """")
            end if
        end if
        generateValue = output
    END FUNCTION
    
    PRIVATE SUB generateArray(val)
        dim item, i
        write("[")
        i = 0
        for each item in val
        if i > 0 then write(",")
        generateValue(item)
        i = i + 1
        next
        write("]")
    END SUB
    
    PRIVATE SUB generateDictionary(val)
        dim keys, i
        innerCall = innerCall + 1
        write("{")
        keys = val.keys
        for i = 0 to uBound(keys)
            if i > 0 then write(",")
            toJSON keys(i), val(keys(i)), true
        next
        write("}")
        innerCall = innerCall - 1
    END SUB
    
    PRIVATE SUB generateRecordset(val)
        dim i
        write("[")
        while not val.eof
            innerCall = innerCall + 1
            write("{")
            for i = 0 to val.fields.count - 1
                if i > 0 then write(",")
                toJSON lCase(val.fields(i).name), val.fields(i).value, true
            next
            write("}")
            val.movenext()
            if not val.eof then write(",")
            innerCall = innerCall - 1
        wend
        write("]")
    END SUB
    
    PRIVATE SUB generateObject(val)
        dim props
        on error resume next
        set props = val.reflect()
        if err = 0 then
            on error goto 0
            innerCall = innerCall + 1
            toJSON empty, props, true
            innerCall = innerCall - 1
        else
            on error goto 0
            write("""" & escape(typename(val)) & """")
        end if
    END SUB
    
    PRIVATE SUB newGeneration()
        output = empty
        innerCall = 0
    end sub
    
    PRIVATE FUNCTION escapequence(digit)
        escapequence = "\u00" + right(padLeft(hex(asc(digit)), 2, 0), 2)
    END FUNCTION
    
    PRIVATE FUNCTION padLeft(value, totalLength, paddingChar)
        padLeft = right(clone(paddingChar, totalLength) & value, totalLength)
    END FUNCTION
    
    PUBLIC FUNCTION clone(byVal str, n)
        dim i
        for i = 1 to n : clone = clone & str : next
    END FUNCTION
    
    PRIVATE SUB write(val)
        if toResponse then
            response.write(val)
        else
            output = output & val
        end if
    END SUB

end class
%> 

调用的时候是这样的

        Set jsonObj=New json
        jsonObj.toResponse=False

代码省略。。。

      jsonStr = jsonObj.toJSON(Empty,json_note,False)
      response.Write(jsonStr)

请问一下,代码中json_note的具体内容是怎样的呢?它是数组还是对象还是字符串?麻烦高手们帮我写一句简单的示例,非常感谢!!!

  • 写回答

5条回答 默认 最新

  • CSDN专家-showbo 2021-06-23 16:25
    关注

      

    json_note可以为数组,字典Scripting.Dictionary,asp的类实例之类的

    帮助到你能点个采纳吗,谢谢~~

    下面就是你要的结构

    
        function createItem(id,age,sex,addtime)
           set item=server.CreateObject("scripting.dictionary")
           item.add "id",id
           item.add "age",age
           item.add "sex",sex
           item.add "addtime",addtime
           set createItem=item
        end function
    
        set dic=server.CreateObject("scripting.dictionary")
        dic.Add "code",0
        dic.Add "msg",""
        dim arr(1)
        set arr(0)=createItem(1,"23","男","2021/6/1 10:42:41")
        set arr(1)=createItem(2,"24","女","2021/6/2 10:42:41")
        set data=server.CreateObject("scripting.dictionary")
        data.Add "item",arr
        data.add "total",2
    
        dic.Add "data",data
    
        jsonStr=jsonObj.toJSON(Empty,dic,False)
    
        
       
        response.Write(jsonStr)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!