如何在没有数组作为键的情况下获取 JSON 数据?
我正在尝试获取 JSON 数据以将其转换为 html,但我有权访问的 JSON 代码没有显示用作数据键的数组。
有谁知道我如何获取这些数据?我的数据值在 HTML 中显示为“未定义”。
示例数据:
[ { "stuff" : {
"categories" : null,
"value-1" : "a string of cool text to display",
"value-2" : 3,
"value-3" : null,
"value-4" : [ ],
"value-5" : 58505,
"value-6" : true,
"value-7" : false,
} },
{ "stuff" : {
"categories" : null,
"value-one" : "another string of cool text to display",
"value-two" : 3,
"value-three" : null,
"value-four" : [ ],
"value-five" : 58505,
"value-six" : true,
"value-seven" : false,
} }
]
示例代码:
$(function() {
$.getJSON( "sample.json", function(data) {
$.each(data, function() {
$('<div></div>')
.hide()
.append('<p>' + this.value-one + '</p>')
.appendTo('#awesome')
.fadeIn();
});
});
});
I'm trying to get JSON data to turn it into html but the JSON code I have access to does not show an array to use as the data key.
Does anyone know how I can go about getting this data? My data values are coming up "undefined" in the HTML.
Sample data:
[ { "stuff" : {
"categories" : null,
"value-1" : "a string of cool text to display",
"value-2" : 3,
"value-3" : null,
"value-4" : [ ],
"value-5" : 58505,
"value-6" : true,
"value-7" : false,
} },
{ "stuff" : {
"categories" : null,
"value-one" : "another string of cool text to display",
"value-two" : 3,
"value-three" : null,
"value-four" : [ ],
"value-five" : 58505,
"value-six" : true,
"value-seven" : false,
} }
]
Sample code:
$(function() {
$.getJSON( "sample.json", function(data) {
$.each(data, function() {
$('<div></div>')
.hide()
.append('<p>' + this.value-one + '</p>')
.appendTo('#awesome')
.fadeIn();
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
更新:当对象键中有
-
时,您无法使用object.value-one
访问它,您必须执行object['value-一个']
。Try:
Update: When object key has
-
in it, you can't access it withobject.value-one
, you must doobject['value-one']
.您没有正确使用 jquery each 。
You are not using jquery each correctly.