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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.