JSON key 称为“true”,无法在 JavaScript 中引用(JSfiddle 示例)
首先,我使用一些在线工具将 Plist(XML 格式)转换为 JSON,这不是问题。我设法从这个相当大的 JSON 文件中挑选出重要的值。有了这些重要信息,我正在重建一个新的 JSON 文件,该文件非常精简,并且包含我可以用于插件的信息 - 我将在稍后创建该文件。
plist 到 JSON 的转换很丑陋。在某些时候,
和
会转换为 JSON,并将其保留在 JSON 中:"false":"" ,
或 "true":"",
。
我正在使用 jQuery
检查 JSfiddle 的示例 jsfiddle 示例
或这里
// Simplified (not really a JSON file, but this will do it for explaining)
var themeJSON = {
"item": {
"false": "",
},
};
// I need to know if its enabled: "true" or disabled: "false"
// function for checking if this is the default option
function checkDefault() {
// true is a keyword!
if (themeJSON.item.true) {
return "1";
// so is false!
} else(themeJSON.item.false) {
return "0";
}
}
也许我使用一些其他函数,例如 find() ?
更新答案: 感谢评论,这就是我现在所拥有的:
function checkDefault() {
if (item.hasOwnProperty("true")) {
return "1";
} else if(item.hasOwnProperty("false")) {
return "0";
}
}
First of all, I converted a Plist(XML formatted) to JSON with some online tool, this isn't the problem. I managed to pick the important values from this rather big JSON file. With this important information I am rebuilding a new JSON file that is very lean and contains information I can use for a plug-in — that I will create later.
The plist conversion to JSON is ugly. At some point <true/>
and <false/>
are converted to JSON, leaving this in the JSON: "false":"",
or "true":"",
.
I am using jQuery
check JSfiddle for an example jsfiddle example
or here
// Simplified (not really a JSON file, but this will do it for explaining)
var themeJSON = {
"item": {
"false": "",
},
};
// I need to know if its enabled: "true" or disabled: "false"
// function for checking if this is the default option
function checkDefault() {
// true is a keyword!
if (themeJSON.item.true) {
return "1";
// so is false!
} else(themeJSON.item.false) {
return "0";
}
}
Maybe I use some other function such as find() ?
updated for answer:
thanks to the comments, this is what I have now:
function checkDefault() {
if (item.hasOwnProperty("true")) {
return "1";
} else if(item.hasOwnProperty("false")) {
return "0";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用属性名称作为字符串:
edit - 注释正确指出,虽然通过字符串值访问属性确实有效,但您的代码在其他方面存在缺陷。如果这些属性确实被赋予了空字符串值,那么您真正需要的是一种测试该属性是否存在的方法,而不是(如这段代码所做的那样)只检查该属性的值属性:
或者,也可以:
对空字符串进行显式检查是否相等:
Try using the property name as a string:
edit — a comment correctly points out that though accessing the properties by string value will indeed work, your code is otherwise flawed. If the properties are really being given empty string values, then what you really need is a way to test whether the property is there at all, and not (as this code does) just check the value of the property:
or, alternatively:
An explicit check for equality against the empty string would do too:
当对象属性的名称是保留关键字时,可以使用数组索引表示法来引用它。
检查
item
是否具有名为false
的属性的方法:这并不理想,因为单个对象可能同时具有
false
属性和>true
属性。When an object property has a name which is a reserved keyword, the array index notation can be used to reference it.
A way of checking whether
item
has a property namedfalse
:This not ideal because a single object could have both a
false
property and atrue
property.在 JS 中,
foo.bar
相当于foo['bar']
。因此:请注意需要
===
作为false == ""
但false !== ""
。另外,我必须挑剔。
themeJSON
不再是 JSON,因为它不是字符串 - 它只是另一个 JavaScript 对象。你不应该混淆这两者。In JS,
foo.bar
is the equivalent offoo['bar']
. Therefose:Note the need for
===
asfalse == ""
butfalse !== ""
.Also, I must nitpick.
themeJSON
is no longer JSON since it's not a string - it's just another JavaScript object. You shouldn't confuse those two.试试这个代码
Try this code