以下是一個在 ASP 中使用 ScriptControl 對象處理 JSON 數據的開源代碼示例:
<%
'假設 JSON 數據
Dim jsonData
jsonData = '{"name":"John","age":30,"hobbies":["reading","coding","gaming"]}'
'使用 ScriptControl 解析 JSON
Set scriptControl = Server.CreateObject("ScriptControl")
scriptControl.Language = "JScript"
Set parsedObject = scriptControl.Eval("(" + jsonData + ")")
'輸出結果
Response.Write("Name: " & parsedObject.name & "<br>")
Response.Write("Age: " & parsedObject.age & "<br>")
Dim hobby
For Each hobby In parsedObject.hobbies
Response.Write("Hobby: " & hobby & "<br>")
Next
Set scriptControl = Nothing
Set parsedObject = Nothing
%>
如果服務器環境允許安裝第三方庫,以下是使用 JSON for VBScript的示例:
<%
'引用 JSON for VBScript 庫
'確保在服務器上正確安裝并注冊了此庫
'假設 JSON 數據
Dim jsonData
jsonData = '{"name":"John","age":30,"hobbies":["reading","coding","gaming"]}'
'解析 JSON
Set jsonObject = JSON.parse(jsonData)
'輸出結果
Response.Write("Name: " & jsonObject.name & "<br>")
Response.Write("Age: " & jsonObject.age & "<br>")
Dim hobby
For Each hobby In jsonObject.hobbies
Response.Write("Hobby: " & hobby & "<br>")
Next
Set jsonObject = Nothing
%>
該文章在 2025/1/1 17:05:08 編輯過