开发者

ASP vbscript fail to detect JSON object

开发者 https://www.devze.com 2023-02-06 10:25 出处:网络
strJSON= getcontent(url) data return as below {\"error\":{\"type\":\"OAuthException\",\"message\":\"Error validating access token.\"}}

strJSON= getcontent(url)

data return as below

{"error":{"type":"OAuthException","message":"Error validating access token."}}

Then I can easily retrieve it by

set return= JSON.parse(strJSON)

From here I can easily retrive all the values inside via below

response.write return.error.type
response.write return.error.message

BUT

If I response.write return.error.otherobjectnotexist

It will return error saying Object doesn't support this property or method: 'otherobjectnotexist'

This is fine when I know exactly what are the objects I will get in return.

In real life scenario, we won't know what is returned, what is not. Especially when json return from 3rd party website.

Example, I use facebook connect to retrieve open graph value of a user and return as json.

Some user filled in "gender", so facebook will return this object. Some user never fill in "gender", so facebook will never return this object.

My program by default will response.write return.gender

If I don't have a way to detect开发者_JS百科 whether object exist or not, and ASP directly throw error making whole program stop, it will be troublesome...

Experts! Any way to resolve this issue?


if I understand you correctly, you want to handle the scenario when trying to access a property that does not exist. You can catch the error and react accordingly eg

' change error handling to carry on in error
on error resume next
' get the property
dim prop: prop = return.gender
if err.number <> 0
    'do something (or nothing)
    err.clear
end if
' reset error handling
on error goto 0


you can test if an object exists

if isObject(NameOfObjectGoesHere) then .....

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号