2

I am having a difficulty retrieving a series of json data from php to my JavaScript, file..

Firstly, I have a series of json data stored in an array in php, and echoing each by wrapping it in the for loop to my JavaScript file,

<?php

$result = array('{"one": "test","two": "test"}','{"three": "test","four": "test"}');

for ($i = 0; $i < count($result); ++$i) {
    echo $result[$i];
}

?>

in my JavaScript,

$.ajax({                              
    url: "visualiser/visualiser_RouteList.php",
    dataType: "JSON",
    async: false,
    success: function(data){
       console.log(data);        
    } 
});

My console does not display anything at all, not recognizing each array element as a json..

but if I send just one array element specifically, for example,

echo $result[0];

then it successfully displays,

Object {one: "test", two: "test"} 

why can't I pass a series of json data in an array from php in my ajax call?

3
  • The PHP output in this case is: {"one": "test","two": "test"}{"three": "test","four": "test"}, which isn't a valid JSON string. See Console for more info on errors. Commented May 25, 2014 at 11:20
  • Based on what documentation/resource/information is it assumed that concatenating valid json strings results in a valid json string? Commented May 25, 2014 at 11:22
  • try this : for ($i = 0; $i < count($result); ++$i) { $ret = $result[$i]; } echo json_encode($ret); Commented May 25, 2014 at 11:22

3 Answers 3

5

It doesn't work because you are generated malformed JSON. The output of your script is

{"one": "test","two": "test"}{"three": "test","four": "test"}

When you access the first element of the array only you get

{"one": "test","two": "test"}

Which is valid.

PHP has json_encode which will do this work for you, so your code would become

$result = array(
    array('one' => 'test', 'two' => 'test'),
    array('three' => 'test', 'four' =>'test')
);

echo json_encode($result);

giving the output

[{"one":"test","two":"test"},{"three":"test","four":"test"}]
Sign up to request clarification or add additional context in comments.

Comments

1

Your code will output this:

{"one": "test","two": "test"}{"three": "test","four": "test"}

This is invalid JSON, so obviously will not work. (Try parsing it with JSON.parse and you'll see.)

You actually need to send the data as an array, so replace your for loop with a simple json_encode call:

echo json_encode($result);

This will output

[{"one": "test","two": "test"},{"three": "test","four": "test"}]

This is valid JSON, and can be parsed into a Javascript array.

3 Comments

are you sitting right next to @danjam :D? It's rare to see answers posted the same second - even more so that are almost the same
@AD7six I know - precisely the same second!
heh, I had to double check I hadn't somehow managed to double post there for a second. You know what they say about great minds :>
0

Also can you this script

function doj($json){
$result = json_encode($json);
return  json_decode($result, true);
}

$json = array(
'{"one": "test","two": "test"}',
'{"three": "test","four": "test"}'
);
$j = doj($json);

foreach($j as $k=>$v){
$extractagain = json_decode($v);
print_r($extractagain);
}

and the output is:

stdClass Object ( [one] => test [two] => test ) stdClass Object ( [three] => test [four] => test )

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.