-1

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

Community
  • 1
  • 1
V. Panchenko
  • 774
  • 1
  • 10
  • 32
  • It is valid or it is not valid. Is JSON Lint *really* says it is valid then you're using different data. Go back to (and correct) the assumptions: *JSON is text*. In the code shown, **there is no JSON**, but rather assignment of a JavaScript object literal. – user2864740 Oct 11 '15 at 23:28
  • 1
    Also, do not create JSON/XML/etc by hand. You're bound to get it wrong at least some of the time or with some data. – user2864740 Oct 11 '15 at 23:32

4 Answers4

4

JSON.parse expects a string to be passed as first argument.

In other words you already have a javascript object. JSON parse is used if you have a string and want to convert to a JavaScript object.

filype
  • 8,034
  • 10
  • 40
  • 66
2

In PHP

    function getJsonFromRows($rows) {
        $json = array();

        foreach ($rows as $rowIndex => $rowData) {
            $tmp = array();
            foreach ($rowData as $columnHeader => $cellValue) {
                $tmp[$columnHeader] =$cellValue;
            }

            $json["$rowIndex"] = $tmp;
        }

        return json_encode($json);
    }

Now if you will do JSON.parse on the response it will convert.

Dinesh Patra
  • 1,125
  • 12
  • 23
0

You are passing an object to JSON.parse instead of a string. Consider if you even need to parse it, as your var response is probably already the object you want.

Andrew Leap
  • 956
  • 4
  • 9
0

As others have been saying, you are trying to parse an object.

JSON.stringify() Converts an object into a string. JSON.parse() Converts a JSON string into an object.

Here is a fiddle showing this is the case with your code. http://jsfiddle.net/wpwelch/39hx5oy0/

<div id="jsonString"></div>
<br />
<div id="jsObject"></div>  

 var test = { "row0": {"year": "2001", "discipline": "some discipline", "theme": "some theme", "pair": "some pair"} , "row1": {"year": "2001", "discipline": "some discipline", "theme": "some theme", "pair": "some pair"} };
    strEcho = JSON.stringify(test);
    objRef = test.row0.year; // Works because var test is already an object
    $("#jsonString").html(strEcho); // Works because .stringify() is working with a valid JSON object.
    $("#jsObject").html(objRef); // This shows the year 2001

In your code, you can edit this line

var data = JSON.parse(response);

to be

var data = response;

and everything will work since you already have a valid object.

buzzsaw
  • 1,476
  • 1
  • 10
  • 14