[英]Issue in accessing JSON object?
Am facing issue in accessing the JSON object : JSON Object am receiving is : 在访问JSON对象时遇到问题:正在接收的JSON对象是:
{"71":"Heart XXX","76":"No Heart YYYY"}
I tried to get the value of 71 and 72 separately and use it ... 我试图分别获取71和72的值并使用它...
but am getting some compile time issue as : Syntax error on token ".71", delete this token 但出现一些编译时问题: 令牌“ .71”的语法错误,删除此令牌
Code: 码:
var map=$("#jsonText").val();
alert(map);
var obj=jQuery.parseJSON(map);
alert("JSON ::"+obj.71);
If am printing obj , am able to view [Object Object]
如果正在打印obj,则可以查看[Object Object]
Can any one out there please help me to find the mistake i did ..I know the question above is asked in many threads in SO . 有人可以帮我找出我做的错误吗..我知道上述问题是在SO的许多线程中提出的。 Below are the few threads i found , but failed when i attempted to implement it .. 以下是我发现的几个线程,但是当我尝试实现它时失败了。
jquery json parsing jQuery json解析
Also tried using the Jquery tutorial given in 还尝试使用中提供的Jquery教程
Its working fine if the key is a String but getting the above error if its a number ... 如果键是字符串,则可以正常工作,但是如果键是数字,则得到上述错误...
Try this: 尝试这个:
alert("JSON ::" + obj[71]);
"71" isn't a valid property identifier: an identifier should start with a letter, the underscore or the dollar sign. “ 71”不是有效的属性标识符:标识符应以字母,下划线或美元符号开头。 You can avoid this problem using square brackets instead. 您可以改用方括号来避免此问题。
Note : everything that's put between square brackets is converted into strings. 注意 :方括号之间的所有内容都将转换为字符串。 Even functions, DOM elements or regular expressions: they're all converted with their toString
methods, or their superclass' toString
. 甚至函数,DOM元素或正则表达式:它们都使用其toString
方法或其超类的toString
。
So 71
there is converted into "71"
. 因此71
转换为"71"
。 If you want a little more performance you can directly use the latter. 如果您想提高性能,可以直接使用后者。 If you don't need it, you can cut some key presses with just 71
. 如果您不需要它,可以只用71
减少一些按键。
改用
alert("JSON ::"+obj["71"]);
according to the rules or javascript
a identifier
should not start by a number so if it starts by a number or for that matter contains spaces and other special charcters then you should access it by using the []
operator and not by .
根据规则或javascript
, identifier
不应以数字开头,因此,如果identifier
以数字开头或为此包含空格和其他特殊字符,则应使用[]
运算符而不是使用.
operator 算子
so obj.71
is invalid but obj["71"]
is 因此obj.71
无效,但obj["71"]
是
try using this site: 尝试使用此网站:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.