3

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?

6 Answers 6

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); 
});
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

0 "1" 1 "1" 2 "1" 3 "825489602V" 4 "Wajira Wanigasekara" APPROVAL LEVEL "1" Employee Name "Wajira Wanigasekara" Employee Number "825489602V" ID "1" WorkFlow Type Code "1" thanks for comment its return this
What is the datatype it returns? Try console.log(typeof(data));
1

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

Comments

1

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

Comments

1

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

Comments

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

for(var key in json.resultArr[0]) {
    console.log( key, json.resultArr[0][key] );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.