Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have a php array like this one :

Array
(
    [0] => banana, peach, cherry
    [1] => strawberry, apple, lime
)

I pass it to Jquery using json_encode($myArray)

In Jquery I receive my array like this : ["banana, peach, cherry","strawberry, apple, lime"]

Now I wanna extract each value : "banana, peach, cherry" & "strawberry, apple, lime"

When I try to use this :

$.each(data, function(key, value){
    alert(value);
});

it alerts me each chars : [ " b a n a n................ etc instead each value.

Do you know why ?

EDIT :

This is how I receive my data from 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); // a function that should extract each value
    }
});
share|improve this question
3  
Try parsing the json with the jQuery-function $.parseJSON(). var parsedData = $.parseJSON(data); $.each(parsedData, function(key, value) { ..... –  ninja Sep 4 '13 at 9:09
    
Your Ajax call should be treating the received data as JSON and converting it to an JS object automatically, but it looks like it is treating it as a plain string and not converting it. Please add to the question your code for your actual Ajax call; this will help us debug the problem for you. –  Spudley Sep 4 '13 at 9:18
    
I have updated my code, in case. –  Xavier C. Sep 4 '13 at 9:44
    
@SergentPepper: Well, what does mix_fruits do? –  Jon Sep 4 '13 at 10:15
    
@Jon : In fact, data returns 2 objects -> error and chunks. mix_fruits should decompose chunks object in order to get the 2 values separatly : "banana, peach, cherry" & "strawberry, apple, lime". –  Xavier C. Sep 4 '13 at 10:26

2 Answers 2

You don't show how you are passing and receiving the data, but somewhere in the process you are making a mistake.

It is evident from the description that data is a string, not an array as it should have been based on the PHP variable. And since the string begins with the characters that make up the JSON representation of your data, this means the JSON is being wrapped into a string instead of parsed as a JavaScript literal.

Assuming that passing/receiving is not done through an AJAX request (in which case jQuery would almost certainly parse the data automatically) I 'm guessing that you are doing this:

var data = '<?php echo json_encode($data); ?>';

while you should instead be doing this:

var data = <?php echo json_encode($data); ?>; // no quotes!
share|improve this answer

You need to parse the JSON object before you can use it,

var jsonObj = jQuery.parseJSON(data);

then to loop through each item use this,

for(var key in jsonObj)
{
    curr = jsonObj[key];
}

And if you want to use non-jquery one,

How to parse JSON in JavaScript

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.