检查我是否从文件中获取JSON,如何?

时间:2014-08-06 09:53:17

标签: json asp-classic

在经典ASP中,我使用aspjson库(http://www.aspjson.com/)。

我有这段代码:

Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.open "GET", "http://www.xxx.xx/json/fetch.php"), False
objXmlHttp.Send

Set mJSON = New aspJSON
mJSON.loadJSON(objXmlHttp.responseText)
set objXmlHttp = nothing

如何检查文件是否存在以及文件是否为JSON?现在,如果文件不是正确的JSON,我会收到错误,我希望能够无误地进行健全性检查。

1 个答案:

答案 0 :(得分:1)

只需使用内置错误处理On Error Resume Next来捕获Err对象中的错误,并跳过触发错误的语句。如果生成错误Err.Number将不等于0。检查错误后,使用On Error Goto 0重置错误处理。

Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.open "GET", "http://www.xxx.xx/json/fetch.php"), False
objXmlHttp.Send

Set mJSON = New aspJSON
On Error Resume Next
mJSON.loadJSON(objXmlHttp.responseText)
If Err.Number <> 0 Then
  'Trap error / do something
  Select Case Err.Number
  Case 1 'No data to load
    'Do something here to handle the error
  Case 2 'Not a collection
    'Do something here to handle the error
  Case Else
    'Unknown error so output it
    Call Response.Write "Unknown Error: " & Err.Number & " (" & Err.Source & ") - " & Err.Description
  End Select
End If
On Error Goto 0
Set objXmlHttp = Nothing