[英]Javascript: Trying to access elements of a JSON array gives me individual characters
$.ajax({
url: "get_cards.php",
type: "GET",
data: {selection:JSON.stringify(selection)},
success: function(data) {
var json = JSON.parse(data);
sessionStorage.setItem("json", JSON.stringify(json));
}
});
Then, in another file I am retrieving the JSON from sessionStorage: 然后,在另一个文件中,我从sessionStorage中检索JSON:
var json = JSON.parse(JSON.stringify(sessionStorage.getItem("json")));
if(json) {
sessionStorage.removeItem("json");
}
This gives me an array of JSON objects, example: [{'name':'Bob',...}]
. 这给了我一个JSON对象数组,例如:
[{'name':'Bob',...}]
。 However, when I try to access the first element of the array: json[0]
, I get '['
and when I try json[0].name
I get undefined
. 但是,当我尝试访问数组的第一个元素:
json[0]
,我得到'['
,当我尝试json[0].name
我得到了undefined
。 The length of json
is reported as 159, so it is counting each individual character as an element. json
的长度报告为159,因此它将每个单独的字符计为元素。
EDIT: When I update to: 编辑:当我更新到:
var json = JSON.parse(sessionStorage.getItem("json"));
if(json) {
sessionStorage.removeItem("json");
}
I get a length of 1 (which is correct), but an error when accessing json[0].name
: 我得到一个长度为1(这是正确的),但访问
json[0].name
时出错:
Uncaught TypeError: Cannot read property '0' of null
at HTMLDocument.<anonymous> (studying.js:10)
at j (jquery.min.js:2)
at k (jquery.min.js:2)
You are stringifying the already stringified json: 您正在对已经字符串化的json进行字符串化:
var json = JSON.parse(JSON.stringify(sessionStorage.getItem("json"))); // wrong !
this should be: 这应该是:
var json = JSON.parse(sessionStorage.getItem("json"));
If you JSON.stringify("foo")
, then you got a quoted string: "\\"foo\\""
. 如果你是
JSON.stringify("foo")
,那么你得到一个带引号的字符串: "\\"foo\\""
。
JSON.stringify()
converts a value to JSON notation representing it:JSON.stringify()
将值转换为表示它的JSON表示法:
- Properties of non-array objects are not guaranteed to be stringified in any particular order.
不保证非数组对象的属性按任何特定顺序进行字符串化。 Do not rely on ordering of properties within the same object within the stringification.
不要依赖于字符串化内同一对象内的属性排序。
Boolean
,Number
, andString
objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.根据传统的转换语义,在字符串化期间将
Boolean
,Number
和String
对象转换为相应的原始值。- If undefined, a function, or a symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).
如果未定义,则在转换期间遇到函数或符号,或者省略(当在对象中找到它时)或者删除为null(当它在数组中找到时)。 JSON.stringify can also just return undefined when passing in "pure" values like JSON.stringify(function(){}) or JSON.stringify(undefined).
传入“纯”值(如JSON.stringify(function(){})或JSON.stringify(undefined)时,JSON.stringify也可以返回undefined。
- All symbol-keyed properties will be completely ignored, even when using the replacer function.
即使使用replacer函数,也将完全忽略所有符号键控属性。
- Non-enumerable properties will be ignored
不可枚举的属性将被忽略
examples :
例子 :
JSON.stringify({}); // '{}' JSON.stringify(true); // 'true' JSON.stringify('foo'); // '"foo"' JSON.stringify([1, 'false', false]); // '[1,"false",false]' JSON.stringify({ x: 5 }); // '{"x":5}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.