从 javascript 数组中获取 Json 对象
我有这样的 Json 对象,
{
" resultArr": [
{
"ID":"1",
"0":"1",
"APPROVAL LEVEL":"1",
"1":"1",
"WorkFlow Type Code":"1",
"2":"1",
"Employee Number":"825489602V",
"3":"825489602V",
"Employee Name":"Wajira Wanigasekara",
"4":"Wajira Wanigasekara"
}
]
}
我正在尝试打印 resultArr 的键和值。
例如,我想打印 ID=1 APPROVAL LEVEL=1.. 就像
我可以使用此代码获取 ID、APPROVAL LEVEL 的值..
$.ajax({
type: "POST",
async:false,
url: "<?php echo url_for('workflow/getApprovalByModuleView') ?>",
data: { viewName: viewName },
dataType: "json",
success: function(data){
alert(data.resultArr[0][3]);
}
});
但我也想打印这些名称...
这意味着我想要打印data.resultArr 数组的 KEY 和 VALUE
我该怎么做?
I have Json object like this
{
" resultArr": [
{
"ID":"1",
"0":"1",
"APPROVAL LEVEL":"1",
"1":"1",
"WorkFlow Type Code":"1",
"2":"1",
"Employee Number":"825489602V",
"3":"825489602V",
"Employee Name":"Wajira Wanigasekara",
"4":"Wajira Wanigasekara"
}
]
}
i am trying to print the key and values of the resultArr.
for example, i want to print ID=1 APPROVAL LEVEL=1.. like that
i can get value of ID,APPROVAL LEVEL.. using this code
$.ajax({
type: "POST",
async:false,
url: "<?php echo url_for('workflow/getApprovalByModuleView') ?>",
data: { viewName: viewName },
dataType: "json",
success: function(data){
alert(data.resultArr[0][3]);
}
});
but i want print the those names also...
that mean i want the print the KEY and VALUE of the data.resultArr array
how can i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
那么你可以使用 key 来选择值。我能想到的唯一方法是循环它们:
Well you use key to select values. The only way I can think of is by looping trough them:
我非常确定您作为成功参数获得的
data
变量并不是您所期望的。首先,弄清楚数据实际包含什么。我建议安装 Firebug 而不是警报写入:
还要检查数据类型尝试:
现在您知道采用哪种格式您的数据确实已返回,您可以继续处理。
I'm pretty sure the
data
variable you get as a parameter for success, is not what you would expect.First of all, figure out, what the data actual contains. I would suggest installing Firebug and instead of alert writing:
Also to check out the datatype try:
Now that you know in which format your data was really returned, you can continue processing it.
javascript:
这将从 javascript 数组中获取键和值。
您遇到的问题是我认为您正在访问两个不同的数组对象。
data.resultArr[0][3]
与data.resultArr[0]["3"]
不同,而data.resultArr[0]["3"]
与 data.resultArr[0 不同][“员工编号”]<代码>。第一个将为您提供批准级别的值,第二个将为您提供第二个员工编号,最后一个将为您提供
员工编号`的值。javascript:
this will get the key and value from an array in javascript.
The problem you have is that I think you're accessing two different array objects.
data.resultArr[0][3]
is NOT the same asdata.resultArr[0]["3"]
which is not the same as data.resultArr[0]["Employee Number"]. The first will give you the VALUE for
Approval Levelthe second will give the the SECOND employee number and the last will give you the value for
Employee Number`.您可以使用以下代码:
You can use the following code:
也许这有帮助:
输出是:“key1,value1”;
Maybe this helps:
Output is: "key1,value1";