Flex 4.5 JSON 在异常之前检查密钥是否存在? [英] Flex 4.5 JSON check for existence of key before exception?

查看:28
本文介绍了Flex 4.5 JSON 在异常之前检查密钥是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Flex 4.5 JSON 因找不到密钥而引发异常之前检查密钥是否存在?我有一些 json 数据,其中一些键并不总是存在.

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.

我正在使用 CallResponder...

I am using CallResponder...

问题是,当您尝试访问 callResponder.lastResponse.key - 并且,例如,key 并不总是出现在您的 json 中时,Flex 将无法解析它.即使您检查 if (callResponder.lastResponse.key) 也会发生这种情况 - 在这种情况下,错误发生在 if 行上

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

(请参阅此处的旧错误转储)

下面提供的答案适用于键是 JSON 中的顶级节点的情况.但是,它似乎无法解析子键的存在.我没想到在解析顶级节点和子节点之间会有区别,但显然情况确实如此.

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.

为了清楚起见(因为我没有提到我在原始问题中尝试解析子键),我在这里创建了一个新问题,专门询问如何解析子键,使用以下方法 -或其他方法,如果需要的话:Flex 4.5 如何检查 JSON 子节点键是否存在(使用 hasOwnProperty 或其他方法)

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)

推荐答案

你如何处理你的 JSON 数据?

How do you process your JSON data?

在我的记忆中,我使用 JSON.decode(String),并得到一个数组.对于每个对象,您可以使用 obj.hasOwnProperty(key) 来检查它是否存在.

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.

这篇关于Flex 4.5 JSON 在异常之前检查密钥是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆