从 javascript 数组中获取 Json 对象

发布于 2024-11-09 07:37:00 字数 1178 浏览 0 评论 0原文

我有这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

走野 2024-11-16 07:37:00

那么你可以使用 key 来选择值。我能想到的唯一方法是循环它们:

$.each(data.resultArr, function(index, value) { 
  alert(index + ': ' + value); 
});

Well you use key to select values. The only way I can think of is by looping trough them:

$.each(data.resultArr, function(index, value) { 
  alert(index + ': ' + value); 
});
会傲 2024-11-16 07:37:00

我非常确定您作为成功参数获得的 data 变量并不是您所期望的。

首先,弄清楚数据实际包含什么。我建议安装 Firebug 而不是警报写入:

console.dir(data);

还要检查数据类型尝试:

console.log(typeof(data));

现在您知道采用哪种格式您的数据确实已返回,您可以继续处理。

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:

console.dir(data);

Also to check out the datatype try:

console.log(typeof(data));

Now that you know in which format your data was really returned, you can continue processing it.

云裳 2024-11-16 07:37:00

javascript:

var array = {"key" : "value", "anotherkey" : "anothervalue"};
for (key in array){
    document.write("For the Element " + "<b>"+array[key]+"</b>" 
  + " Key value is  " +"<b>"+key+"</b>"+"<br>");
}

这将从 javascript 数组中获取键和值。

您遇到的问题是我认为您正在访问两个不同的数组对象。

data.resultArr[0][3]data.resultArr[0]["3"] 不同,而 data.resultArr[0]["3"] 与 data.resultArr[0 不同][“员工编号”]<代码>。第一个将为您提供批准级别的值,第二个将为您提供第二个员工编号,最后一个将为您提供员工编号`的值。

javascript:

var array = {"key" : "value", "anotherkey" : "anothervalue"};
for (key in array){
    document.write("For the Element " + "<b>"+array[key]+"</b>" 
  + " Key value is  " +"<b>"+key+"</b>"+"<br>");
}

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 as data.resultArr[0]["3"] which is not the same as data.resultArr[0]["Employee Number"]. The first will give you the VALUE forApproval Levelthe second will give the the SECOND employee number and the last will give you the value forEmployee Number`.

一生独一 2024-11-16 07:37:00

您可以使用以下代码:

var 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"}]};

var data = json.resultArr[0];
for (var key in data){
    document.write(key+":" + data[key]);
}

You can use the following code:

var 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"}]};

var data = json.resultArr[0];
for (var key in data){
    document.write(key+":" + data[key]);
}
抹茶夏天i‖ 2024-11-16 07:37:00

也许这有帮助:

function getArrayKeyAndValue(array, index){
    var count = 0;
    for (key in array){
        if(++count == index)
            return [key, array[key]];
    }      
}

var array = {"key1" : "value1", "key2" : "value2", "key3" : "value3"};
document.write(getArrayKeyAndValue(array, 1));

输出是:“key1,value1”;

Maybe this helps:

function getArrayKeyAndValue(array, index){
    var count = 0;
    for (key in array){
        if(++count == index)
            return [key, array[key]];
    }      
}

var array = {"key1" : "value1", "key2" : "value2", "key3" : "value3"};
document.write(getArrayKeyAndValue(array, 1));

Output is: "key1,value1";

愁杀 2024-11-16 07:37:00
var 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"}]};

for(var key in json.resultArr[0]) {
    console.log( key, json.resultArr[0][key] );
}
var 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"}]};

for(var key in json.resultArr[0]) {
    console.log( key, json.resultArr[0][key] );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文