How to read Json_encoded php array from jquery? -
i have php array 1 :
array ( [0] => banana, peach, cherry [1] => strawberry, apple, lime )
i pass jquery using json_encode($myarray)
in jquery receive array : ["banana, peach, cherry","strawberry, apple, lime"]
now wanna extract each value : "banana, peach, cherry" & "strawberry, apple, lime"
when try use :
$.each(data, function(key, value){ alert(value); });
it alerts me each chars : [ " b n n................ etc instead each value.
do know why ?
edit :
this how receive data php :
$.post('ajax/fruits.php', function(data) { var obj = $.parsejson(data); var chunks = obj['chunks'] // gives me : ["banana, peach, cherry","strawberry, apple, lime"] if (obj['error']==0) { mix_fruits(chunks); // function should extract each value } });
you don't show how passing , receiving data
, somewhere in process making mistake.
it evident description data
string, not array should have been based on php variable. , since string begins characters make json representation of data, means json being wrapped string instead of parsed javascript literal.
assuming passing/receiving not done through ajax request (in case jquery parse data automatically) 'm guessing doing this:
var data = '<?php echo json_encode($data); ?>';
while should instead doing this:
var data = <?php echo json_encode($data); ?>; // no quotes!
Comments
Post a Comment