I write web site using Yii framework. I need get all data from DB in Yii, and I wrote some code for this:
var response =
<?php
function getAllDataTable() {
$connection = Yii::app()->db;
$sql = "SELECT * FROM data";
$command = $connection->createCommand($sql);
$rows = $command->queryAll();
return $rows;
}
function getJsonFromRows($rows) {
$json = "{ ";
foreach ($rows as $rowIndex => $rowData) {
if ($rowIndex !== 0)
$json .= ", ";
$json .= "\"row".$rowIndex."\": {";
$colIndex = 0;
foreach ($rowData as $columnHeader => $cellValue) {
if ($colIndex++ !== 0)
$json .= ", ";
$json .= "\"".$columnHeader."\"".": "."\"".$cellValue."\"";
}
$json .= "} ";
}
$json.="}";
return $json;
}
$rows = getAllDataTable();
echo getJsonFromRows($rows);
?>;
var data = JSON.parse(response);
console.log("data: \"" + data + "\"");
I try to use JSON to do it. This is code from debugger:
var response = { "row0": {"year": "2001", "discipline": "some discipline", "theme": "some theme", "pair": "some pair"} , "row1": {"year": "2001", "discipline": "some discipline", "theme": "some theme", "pair": "some pair"} };
var data = JSON.parse(response);
console.log("data: \"" + data + "\"");
This is errors, which I got:
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data material:53:18
Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead jquery-1.9.1.min.js:1:0
Use of getPreventDefault() is deprecated. Use defaultPrevented instead.
I check result of php script on the many sites such this: https://jsonformatter.curiousconcept.com/ my json object is correct but JSON.parse(jsonObject) return error. Why?
P.S. This theme isn't duplicate, I see all this posts: JSON Lint says it's valid but JSON.parse throws error jQuery IE9 JSON.SyntaxError parseerror, but JSON is valid JSON.parse Error on Valid JSON check if required JSON is valid - node JSON.parse error on a seemingly valid JSON