user3253289
user3253289

Reputation: 63

how to get correct data display from json

i have my json sent from the php file as

echo json_encode(array('uname'=>$fname,'Oname'=>$pi['name'],'osize'=>$size));

iam looping through it on the client side with the following jquery,but it does not display all records in the list.how can i make it work effectively

function work(i,fly,response){
var cookedJSON=[];cookedJSON.push({ uname : response.uname , Oname : response.Oname , osize :response.osize });
             _outfilenames=[];
    $.each(cookedJSON, function(i,arr) {

          _outfilenames.push("<li id='group_0_file_"+i+"'class='image-list'>" +"<div class='controls' title='remove file'><a href='#' class='image-list' data-filename='"+arr.uname +"'><img src='stop.png' ></a></div>"+   "<span class='filename'>" +arr.Oname+ "</span> "  +"<span class='details'>size:</strong>"+arr.osize +" </span>" +"</li>");//}
    });  // close each()
    $(_outfilenames.join('')).appendTo(f_list);

what am i doing wrong and what can I do to put it right????

Upvotes: 1

Views: 75

Answers (1)

user2845946
user2845946

Reputation: 1815

Your cookedJSON is an array that stores only one JSON in it. To retrieve this JSON properties, you should use:

cookedJSON[0].uname;
cookedJSON[0].oname;
cookedJSON[0].osize;

You may also use:

cookedJSON[0]['uname'];
cookedJSON[0]['oname'];
cookedJSON[0]['osize'];

If you however use:

cookedJSON.uname;
cookedJSON.oname;
cookedJSON.osize;

You would get undefined in the console log.

Upvotes: 1

Related Questions