经典 ASP:如何使用 AX json 实现将 RecordSet 转换为 json 表示法 [英] Classic ASP: how to convert a RecordSet to json notation using AXE json implementation

查看:20
本文介绍了经典 ASP:如何使用 AX json 实现将 RecordSet 转换为 json 表示法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery 和其他一些工具使用 ajax 做一个应用程序,在某些部分我想使用经典的 ASP 后端通过 ajax 检索数据,我看到在 AX (Asp极限版)框架,我用过,但目前我不明白如何使用它.

编辑:基于(也就是我还没有测试过)但现在我已经完成了这个.

使用记录集对象测试我的更改会导致此输出

<代码>"{{"clave":"BC","descripcion":"Cal"},{"clave":"CT","descripcion":"Center"},{"clave":"NE","descripcion":"Norw"},{"clave":"NO","descripcion":"Nore"},{"clave":"NT","descripcion":"North"},{"clave":"OC","descripcion":"East"},{"clave":"OR","descripcion":"West"},{"clave":"PE","descripcion":"Pen"},{"clave":"SE","descripcion":"Southe"},{"clave":"ZM","descripcion":"Met"}}"

遗留问题

我留下的问题有点像黑客.

  • 输出在开始处有 (") 字符是可以的,并且结束和我其他一切都有逃避 sencuences ??或者它的东西这不应该发生.

  • 这种黑客行为真的是错误的......我可以做得更好,在这方面什么样的情况?

  • 我的结论或论点有问题??

i'm doing an application with ajax using jQuery and some other tools, and in some part i want to retrieve data with ajax using a classic ASP backend, i saw that exists a good implementation of a JSON class in AXE (Asp extreme edition) framework, and i used it but currently i don't understand how to use it well.

Edit: based on the correct answer of JSON.Stringify fails on Scripting.Dictionary objects Thread, i decided to make a custom function to process Recordsets.

Edit 2: Now i'm losing the value data when call JSON.stringify inside function JSONStringify(object).

when the Recordset is passed as value to JSONStringify everything is ok but when JSON.stringify is executed, the "value" parameter that must contain the recordset becomes undefined

What i'm expecting (example)

passing a Recordset with from a SQL query SELECT name, tel FROM users a see an output like this

[
    {"name":"Jonh Smith", "tel":"12345678"},
    {"name":"April Michelson", "tel":"77788802"},
    ...
]

passing a Dictionary and see something similar based in the elements declared in dictionary.

{
   "element1":"value1",
   "element2":"value2",
   "element3":"value3",
   "element4":"value4",
   "element5":"value5"
}

and if i like to support other type object i can do it expanding the function

Source Code

getcatalogos.asp

<!--#include file="../includes/conexion.asp" -->
<!--#include file="../includes/json2.asp" -->
<!--#include file="../includes/json-stringify-parser.asp" -->
<%
Response.ContentType = "application/json"
dim aVals(2)

function getCatalogo(tipo, params)
    Dim oConn,oCmd,sSQL,oRs,cont2
    Dim aData,oPar,cont
    dim Info 

    set oConn = Server.CreateObject("ADODB.Connection")
    set oCmd = Server.CreateObject("ADODB.Command")

    sWhere = ""

    oConn.ConnectionString = strcon
    oConn.Open
    Set oCmd.ActiveConnection = oConn

    select case tipo
        case "g"
            sSQL = " SELECT cve_gr, descr FROM gr ORDER BY descr ;"
        case "z" 
            sSQL = " SELECT cve_zn, descr FROM zn WHERE cve_gr = ? ORDER BY descr ;"
            if IsArray(params) Then
                Set oPar=oCmd.CreateParameter (params(0),129,1,2,params(1))
                oCmd.Parameters.Append(oPar)
            End if
        case else
            getCatalogo = false
            exit function
    end select

    oCmd.CommandText = sSQL
    Set oRs = oCmd.Execute()
    if Not oRs.EOF Then
        response.write(JSONStringify(oRs))
        getCatalogo = true
    else
        getCatalogo = false
    end if
    oConn.Close
end function

aVals(0) = "cve_gr"
aVals(1) = request.querystring("gr")
if Not getCatalogo(request.querystring("t"),aVals) Then
    %>error<%
end if

%>

json-stringify-parser.asp

<!--#include file="vbsTyper.asp" -->
<script runat="server" language="JScript">

    function JSONStringify(object) {
        VBSTypeName(object);
        return JSON.stringify(object,stringifyData);
    }

    function stringifyData(holder, key, value) {
        var sType = '';
        var result;

        //response.write('pre...holder=' + holder + ',key=' + key + ',value=' + value);
        sType = VBSTypeName(value);
        //response.write('post =' + sType);

        //response.write(sType);
        switch(sType){
            case 'Dictionary':
                result = '{';
                for(var enr = new Enumerator(value); !enr.atEnd(); enr.moveNext()){
                    key = enr.item();
                    result += '"' + key + '": ' + JSON.stringify(value.Item(key));
                };
                result += '}';
                return(result);
                break;
            case 'Recordset':
                response.write('here!!!');
                var sTemp = '';
                result = '{';
                while(!value.EOF){
                    if(Len(result) > 0){
                        result += ',';
                    }
                    result += '{';
                    for (var i = value.Fields.Count - 1; i >= 0; i--){
                        if(len(sTemp) > 0){
                            sTemp += ',';
                        }
                        sTemp += '"' + value.Fields(i).name + '":' + JSON.stringify( value.Fields(i).value);
                    };
                    result += '}';
                }   
                result += '}';
                return result;
                break;
            default:
                //response.write(sType);
                return(value);
        }     
        // return the value to let it be processed in the usual way
        return result;
   }

</script>

vbsTyper.asp

<%
Function VBSTypeName(Obj)
    dim sType 
    sType = Cstr(TypeName(Obj))
    response.write(sType)
    VBSTypeName = sType
End Function
%>

解决方案

kind of achieve it...

Short version: i had to modify the json2.asp and hack the stringify() function definition to make it works.

Long Version

later of see every line of code and giving up on the problem. i decided to take a look into json2.asp (AXE Framework) and try to see what it happening there.

look what i see:

from the lines 682 to 687 theres a validation if a custom stringy parser is found but later doesn't do anything ... only returns the Object value.

there's speculation from here, because i don't understand well how works every Javascript implementation, but implying that the original code runs well, that a standart javascript interpreter (read everything else but microsoft here) will force a serialization of the object in this sutuation, but the JScript interpreter try to parse the object and pass the value as null. resulting in that every function that uses a custom stringyfier can't read the object passed in the function.

what i did to resolve ok i inserted this chunk of code before line 688, forcing to execute the custom stringyfier with value directly passed as argument avoiding the implicit parsing.

        // Hack & patch to deliver the stringify-ing of the object correctly
        // IDK if this is CORRECT or dont but it works in VbScript
        if(replacer){
            var textval = rep(this,'',value);
            value = textval;
        }

later i had to do some changes in json-stringify-parser.asp because i had to fix some bugs resulting in this code

<!--#include file="vbsTyper.asp" -->
<script runat="server" language="JScript">

    function JSONStringify(object) {
        //VBSTypeName(object);
        return JSON.stringify(object,stringifyData,4);
    }

    function stringifyData(holder, key, value) {
        var sType = '';
        var result;

        //response.write('pre...holder=' + holder + ',key=' + key + ',value=' + value);
        sType = VBSTypeName(value);
        //response.write('post =' + sType);

        //response.write(sType);
        switch(sType){
            case 'Dictionary':
                result = '{';
                for(var enr = new Enumerator(value); !enr.atEnd(); enr.moveNext()){
                    key = enr.item();
                    result += '"' + key + '": ' + JSON.stringify(value.Item(key));
                };
                result += '}';
                return(result);
                break;
            case 'Recordset':
                //response.write('here!!!');
                var sTemp;
                result = '';
                while(!value.EOF){
                    if(result.length > 0){
                        result += ',';
                    }
                    result += '{';
                    sTemp=''
                    for (var i = 0; i < value.fields.Count; i++){
                        if(sTemp.length > 0){
                            sTemp += ',';
                        }
                        //response.write("i=" + i + ",");
                        sTemp += '"' + value.fields.item(i).name + '":' + JSON.stringify( value.fields.item(i).value);
                    };
                    result += sTemp + '}';
                    value.moveNext();
                }   
                result = '{' + result  + '}';
                return result;
                break;
            default:
                //response.write(sType);
                return(value);
        }     
        // return the value to let it be processed in the usual way
        return result;
   }

</script>

the part to parse a Recordset works, the part to parse a dictionary is same like the shown in JSON.Stringify fails on Scripting.Dictionary objects (a.k.a. i haven't tested yet) but for now i'm done with this.

testing my changes with a recordset object results in this output

"{
    {"clave":"BC","descripcion":"Cal"},
    {"clave":"CT","descripcion":"Center"},
    {"clave":"NE","descripcion":"Norw"},
    {"clave":"NO","descripcion":"Nore"},
    {"clave":"NT","descripcion":"North"},
    {"clave":"OC","descripcion":"East"},
    {"clave":"OR","descripcion":"West"},
    {"clave":"PE","descripcion":"Pen"},
    {"clave":"SE","descripcion":"Southe"},
    {"clave":"ZM","descripcion":"Met"}
}"

Questions Left

question that i have left withis kind of hack.

  • it's Ok that the output has (") character at the beggining and the end and i everything else has escape sencuences ?? or it's something that should not occurs.

  • this kind of hack is really wrong ... i can do it better, in this kind of situation ?

  • it's something wrong in my conclusion or arguments ??

这篇关于经典 ASP:如何使用 AX json 实现将 RecordSet 转换为 json 表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆