admin管理员组

文章数量:1294662

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?

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?

Share edited Jul 5, 2013 at 6:16 Roshan Wijesena asked May 25, 2011 at 10:03 Roshan WijesenaRoshan Wijesena 3,1369 gold badges40 silver badges58 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

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); 
});

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.

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`.

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]);
}

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";

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] );
}

本文标签: jqueryJson Object fetch from javascript arrayStack Overflow