Extract Json object from javascript array

I have a 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 resultArr values.

for example, I want to print ID = 1 APPROVAL LEVEL = 1 .. like this

i can get the value of ID, APPROVAL LEVEL .. with 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 also want to print these names ...

which means i want to print the KEY and VALUE of the data.resultArr array

How can i do this?

+3
source share
6 answers

Well, you use a key to select values. The only way I can think of is to focus on them:

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

I am sure that the variable datayou will receive as a parameter for success is not what you expect.

, , . Firebug :

console.dir(data);

, try:

console.log(typeof(data));

, , , .

+2

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] [ " " ] . The first will give you the VALUE for the second will give the the SECOND employee number and the last will give you the value for `.

+1

:

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

, :

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: "key1, value1";

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

All Articles