如何在经典ASP中将多维数组转换为JSON?

时间:2012-01-10 16:54:09

标签: json asp-classic

我在Classical ASP中有这个代码:

    dim arr(2,3)
arr(0,0) = "proc"
arr(1,0) = "code"
arr(2,0) = "hour"

arr(0,1) = "11111"
arr(1,1) = "01"
arr(2,1) = "5"

arr(0,2) = "22222"
arr(1,2) = "02"
arr(2,2) = "6"

arr(0,3) = "33333"
arr(1,3) = "03"
arr(2,3) = "4"

我想在Classical ASP中改编如下面的JSON:

{"process":[
                {"proc" : "11111", "code" : "01", "hour":"5"},
                {"proc" : "22222", "code" : "02", "hour":"6"},
                {"proc" : "33333", "code" : "03", "hour":"4"}
               ]
    }

任何人都可以帮助我?

1 个答案:

答案 0 :(得分:3)

这是一个函数,它将为你正在使用的类型记录数组执行json的序列化:

Function RecordArrayToJSON(arr)

    ReDim arrObj(UBound(arr, 2) - 1)

    Dim i, j
    For i = 1 To UBound(arr, 2)
        ReDim arrProp(UBound(arr, 1))
        For j = 0 To UBound(arr, 1)
            arrProp(j) = """" & arr(j, 0) & """ : """ & arr(j, i) & """"
        Next 

        arrObj(i-1) = "{" & Join(arrProp, ", ") & "}"
    Next

    RecordArrayToJSON = "[" + Join(arrObj, ",") + "]"
End Function

您将获得最终的JSON:

 Dim json : json = "{""process"":" + RecordArrayToJSON(arr) + "}"

假设所有属性值都是字符串,在您的示例中就是这种情况。