开发者

Json Object fetch from javascript array

开发者 https://www.devze.com 2023-03-08 01:42 出处:网络
I have Json object like this { \"resultArr\": [ { \"ID\":\"1\", \"0\":\"1\", \"APPROVAL LEVEL\":\"1\", \"1\":\"1\",

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') ?>",
                    开发者_StackOverflow中文版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?


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

精彩评论

暂无评论...
验证码 换一张
取 消