使用jQuery打印JSON对象 [英] Print JSON object with jQuery
问题描述
我有一些如下所示的JSON数据:
I have some JSON data looking like this:
"Extras": {
"T01": "Value 1",
"T02": "Value 2",
"T03": "Value 3",
// etc.
}
我需要列表中的每个值,因此我尝试了以下方法:
I need each of these values in a list, and so I've tried this:
$.each(data.result, function(i){
$("#result").append('<li>'+data.result.Extras[]+'</li>');
});
这显然行不通,但我似乎无法弄清楚该怎么办.我试过data.result.Extras
没有运气(因为我刚得到[object][Object]
).
Which obviously doesn't work, I just can't seem to figure out what to do. I've tried data.result.Extras
with no luck (as I just get [object][Object]
).
有什么想法我可以做得到列表中的所有值吗?谢谢!
Any ideas what I can do to get all of the values in a list? Thanks!
推荐答案
您正在迭代data.result
,我相信您需要迭代Extras
.
You are iterating the data.result
, I believe you need to iterate the Extras
.
$.each(data.result.Extras, function(k, v){
$("#result").append('<li>'+v+'</li>');
});
注意:在上面的函数中,k
是键,v
是对象Extras
中的值.例如:对于第一个迭代,k
将是T01
,而v
将是Value 1
.
Note: In the above function k
is key and v
is value in the object Extras
. For ex: For the first iterate k
would be T01
and v
would be Value 1
.
以上内容应产生并输出
<ul id="result">
<li>Value 1</li>
<li>Value 2</li>
<li>Value 3</li>
</ul>
这篇关于使用jQuery打印JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!