Flex 4.5 JSON check for existence of key before ex

2019-08-26 10:24发布

问题:

Is it possible to check for the existence of a key before Flex 4.5 JSON throws an exception for key not found? I have some json data where some keys are not always present.

I am using CallResponder...

The issue is that when you try accessing callResponder.lastResponse.key - and, say, key is not always present in your json, Flex won't be able to parse it. This happens even if you check if (callResponder.lastResponse.key) - the error occurs on the if line, in that case


(See old error dump here)


The answer provided below works for cases where the keys are top level nodes in the JSON. However, its seems unable to parse existence for child keys. I had not expected there to be a difference between parsing for top level nodes and child nodes, but apparently that is the case.

For clarity's sake (as I did not mention I am trying to parse for child keys in my original question), I've created a new question here that specifically asks how to parse for child keys, using the method below -- or another method, if need be: Flex 4.5 How do you check for JSON child node key existence (using hasOwnProperty or other methods)

回答1:

How do you process your JSON data?

In my memory, I use JSON.decode(String), and got an array. For each object, you can use obj.hasOwnProperty(key) to check whether it's present.