JSON and PHP question

I have this JSON data
{
    "data": [
        [
            "Tiger Nixon", "System Architect", "Edinburgh", "61", "2011/04/25", "$320,800"
        ]
    ]
}

Open in new window

and it running fine when I used with this
        $('#example1').dataTable( {
            "ajax": '../../api/get_data.php'
        });

Open in new window


Then I use PHP coding to encode the JSON data, and the coding is as below. When I try to run it, it says Datatables warning etc...
Is the below code is wrong? Appreciate any feedback from experts in this forum.

<?php
echo "{";
echo "'data': [";
echo "[";
echo "'Garrett Winters', 'Accountant', 'Tokyo', '63', '2011/07/25', '$170,750'";
echo "]";
echo "]";
echo "}";
?>

Open in new window

najibsaidinAsked:
Who is Participating?
 
Slick812Commented:
greetings najibsaidin, , you do not really "use PHP coding to encode the JSON data" in your example, if you do NOT have PHP variables and operations to change the variables (like do a SELECT from a database Table), then you have no reason to use all of those echo.

But, you can change over to single quotes, and just copy the working text into it -
echo '{
"data": [
    ["Tiger Nixon", "System Architect", "Edinburgh", "61", "2011/04/25", "$320,800" ]
  ]
}';

Open in new window

0
 
Ray PaseurCommented:
PHP has built-in functionality for handling JSON.  The built-in functions will let you test the string programmatically to see if it is valid JSON.  Here's a link to the man pages:
http://php.net/manual/en/book.json.php

It looks fine to me.
http://iconoun.com/demo/temp_najibsaidin.php

<?php // demo/temp_najibsaidin.php

/**
 * http://php.net/manual/en/book.json.php
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28666150.html
 */
error_reporting(E_ALL);

$jso = <<<EOD
{
    "data": [
        [
            "Tiger Nixon", "System Architect", "Edinburgh", "61", "2011/04/25", "$320,800"
        ]
    ]
}
EOD;

// IS IT VALID JSON?
$obj = json_decode($jso);

// YES
echo '<pre>';
var_dump($obj);

Open in new window

0
 
RobOwner (Aidellio)Commented:
easiest thing to do in your php is:

<?php

<?php

$mydata = array('data'=>array(array('Garrett Winters', 'Accountant', 'Tokyo', '63', '2011/07/25', '$170,750')));

header("application/json");
echo json_encode($mydata);

?>

Open in new window




?>
0
 
Ray PaseurCommented:
Since this question was posted, we have gotten an article about PHP and JSON.  You may find it useful.
http://www.experts-exchange.com/articles/22519/Understanding-JSON-in-PHP-and-JavaScript-Applications.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.