[英]JSON key is called “true”, unable to reference in JavaScript(JSfiddle example)
First of all, I converted a Plist(XML formatted) to JSON with some online tool, this isn't the problem. 首先,我使用一些在线工具将Plist(XML格式)转换为JSON,这不是问题。 I managed to pick the important values from this rather big JSON file.
我设法从这个相当大的JSON文件中选择了重要的值。 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.
借助这些重要信息,我将重建一个非常精简的新JSON文件,其中包含可用于插件的信息-我将在以后创建。
The plist conversion to JSON is ugly. 从plist转换为JSON很难。 At some point
<true/>
and <false/>
are converted to JSON, leaving this in the JSON: "false":"",
or "true":"",
. 在某些时候,
<true/>
和<false/>
会转换为JSON,并将其保留在JSON中: "false":"",
或"true":"",
,。
I am using jQuery 我正在使用jQuery
check JSfiddle for an example jsfiddle example 检查JSfiddle以获得jsfiddle示例
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() ? 也许我使用其他功能,例如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";
}
}
Try using the property name as a string: 尝试使用属性名称作为字符串:
if (themeJSON.item['true']) {
return '1';
}
else if (themeJSON.item['false']) {
return "0";
}
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:
如果确实为属性提供了空字符串值,那么您真正需要的是一种测试属性是否存在的方法,而不是(如此代码一样)仅检查属性的值 :
if (typeof themeJSON.item['true'] !== 'undefined') { ... }
or, alternatively: 或者,或者:
if ('true' in themeJSON.item) { ... }
An explicit check for equality against the empty string would do too: 对空字符串进行相等性的显式检查也可以:
if (themeJSON.item['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 named false
: 一种检查
item
是否具有名为false
的属性的方法:
> themeJSON.item.hasOwnProperty("false");
true
This not ideal because a single object could have both a false
property and a true
property. 这不是理想的,因为单个对象可能同时具有
false
属性和true
属性。
In JS, foo.bar
is the equivalent of foo['bar']
. 在JS中,
foo.bar
等效于foo['bar']
。 Therefose: Therefose:
if (themeJSON.item['true'] === "")
Note the need for ===
as false == ""
but false !== ""
. 请注意,需要
===
为false == ""
但为false !== ""
。
Also, I must nitpick. 另外,我必须挑剔。
themeJSON
is no longer JSON since it's not a string - it's just another JavaScript object. themeJSON
不再是JSON,因为它不是字符串,而只是另一个JavaScript对象。 You shouldn't confuse those two. 您不应混淆这两个。
Try this code 试试这个代码
function checkDefault() {
// true is a keyword!
if ("true" in themeJSON.item) {
return "1";
// so is false!
} else if ("false" in themeJSON.item) {
return "0";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.