解析经典ASP中的json字符串

时间:2013-11-24 00:56:04

标签: json asp-classic vbscript

我试图解析经典asp中的json字符串。找到了stackoverflow上引用的有用脚本:https://github.com/douglascrockford/JSON-js/blob/master/json2.js

调用'parse'函数时出错:

<script language="JScript" runat="server" src="json2.js"></script>

'line 144 of my script


Set myJSON = JSON.parse(versionsStr)


Microsoft VBScript runtime error '800a01a8' 

Object required: '[string: "[{"self":"https://dc"]' 

/libs/asp/common/jira_api.asp, line 144 

这是我的json字符串(versionStr):

 [{"self":"https://server.com:8343/rest/api/2/version/10300","id":"10300","description":"baseline version","name":"1.0","archived":false,"released":true,"releaseDate":"2013-11-07","userReleaseDate":"07/Nov/13"},{"self":"https://server.com:8343/rest/api/2/version/10301","id":"10301","description":"sample version","name":"1.1.0","archived":false,"released":false},{"self":"https://server.com:8343/rest/api/2/version/10302","id":"10302","name":"3.0.0","archived":false,"released":false}]

我猜我正在使用的脚本是期待一个json对象,但是我对jira API的调用是在回转json字符串。有人有解决方案将json字符串解析为经典的asp数组吗?

1 个答案:

答案 0 :(得分:0)

你是对的。您可以从vbscript代码调用javascript代码。你可以使用json2.js脚本,但你不能做的是将数组从javascript传递给vbscript并使用索引访问它们。

将其保存为test.wsf并使用cscript test.wsf运行(确保json2.js位于同一目录中)。

<?XML version="1.0" standalone="yes" encoding="utf-8" ?>
<package>
<job id="test" prompt="no">
<?job error="true" debug="true" timeout="10" ?>

<script language="Javascript" src="json2.js" />

<script id="main" language="VBScript">
<![CDATA[

    Dim versionStr
        versionStr ="[{""self"":""https://server.com:8343/rest/api/2/version/10300"",""id"":""10300"",""description"":""baseline version"",""name"":""1.0"",""archived"":false,""released"":true,""releaseDate"":""2013-11-07"",""userReleaseDate"":""07/Nov/13""}" + _ 
                    ",{""self"":""https://server.com:8343/rest/api/2/version/10301"",""id"":""10301"",""description"":""sample version"",""name"":""1.1.0"",""archived"":false,""released"":false}" + _ 
                    ",{""self"":""https://server.com:8343/rest/api/2/version/10302"",""id"":""10302"",""name"":""3.0.0"",""archived"":false,""released"":false}]"

    Dim myJSON 
        Set myJSON = JSON.parse(versionStr)

    Dim numVersions
        numVersions = myJSON.length

    Dim i, version
        For i = 1 to numVersions
            Set version = myJSON.shift()
            WScript.Echo version.self
        Next


]]>
</script>

</job>
</package>