json_encode返回无效的json代码


json_encode returns invalid JSON code

我正在用PHP创建一些似乎无效的JSON数据。我正在尝试将谷歌API集成到我的代码中。

<?php
$con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysql_select_db("mobiledb", $con); 
// The Chart table contains two fields: weekly_task and percentage
// This example will display a pie chart. If you need other charts such as a Bar chart, you will need to modify the code a little to make it work with bar chart and other charts
$sth = mysql_query("SELECT `id`, `Q1`, `Q2` FROM `table2` WHERE `id`=8710058770");
/*
---------------------------
example data: Table (Chart)
--------------------------
weekly_task     percentage  marks
Sleep           30          60
Watching Movie  40          80
work            44          90
*/
$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(
    // Labels for your chart, these represent the column titles
    // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
    array('label' => 'id', 'type' => 'string'),
    array('label' => 'Q1', 'type' => 'number'),
    array('label' => 'Q2', 'type' => 'number')
);
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $temp = array();
    // the following line will be used to slice the Pie chart
    $temp[] = array('v' => (string) $r['id']);
    // Values of each slice
    $temp[] = array('v' => (int) $r['Q1'], 'f' => (int) $r['Q2']);
    $rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
echo $jsonTable;
?>

MY Json输出和登记http://jsonlint.com/

   {
cols: [
{
label: "id",
type: "string"
},
{
label: "Q1",
type: "number"
},
{
label: "Q2",
type: "number"
}
],
rows: [
{
c: [
{
v: "8710058770"
},
{
v: 35,
f: 40
}
]
},
{
c: [
{
v: "8710058770"
},
{
v: 60,
f: 70
}
]
},
{
c: [
{
v: "8710058770"
},
{
v: 75,
f: 85
}
]
}
]
}

当我验证时,我可以看到错误第1行分析错误:{cols:[{
-----^应为"STRING"、"}"

print_r($table)输出

Array
(
    [cols] => Array
        (
            [0] => Array
                (
                    [label] => id
                    [type] => string
                )
            [1] => Array
                (
                    [label] => Q1
                    [type] => number
                )
            [2] => Array
                (
                    [label] => Q2
                    [type] => number
                )
        )
    [rows] => Array
        (
            [0] => Array
                (
                    [c] => Array
                        (
                            [0] => Array
                                (
                                    [v] => 8710058770
                                )
                            [1] => Array
                                (
                                    [v] => 35
                                )
                            [2] => Array
                                (
                                    [v] => 40
                                )
                        )
                )
            [1] => Array
                (
                    [c] => Array
                        (
                            [0] => Array
                                (
                                    [v] => 8710058770
                                )
                            [1] => Array
                                (
                                    [v] => 60
                                )
                            [2] => Array
                                (
                                    [v] => 70
                                )
                        )
                )
            [2] => Array
                (
                    [c] => Array
                        (
                            [0] => Array
                                (
                                    [v] => 8710058770
                                )
                            [1] => Array
                                (
                                    [v] => 75
                                )
                            [2] => Array
                                (
                                    [v] => 85
                                )
                        )
                )
        )
)
{"cols":[{"label":"id","type":"string"},{"label":"Q1","type":"number"},{"label":"Q2","type":"number"}],"rows":[{"c":[{"v":"8710058770"},{"v":35},{"v":40}]},{"c":[{"v":"8710058770"},{"v":60},{"v":70}]},{"c":[{"v":"8710058770"},{"v":75},{"v":85}]}]}

<?php之前,php文件的开头有一些空白,这可能会导致输出被错误解释。还要确保你有

header("Content-Type: application/json");

在您回显json之前。

实际上,问题是由于字符串值没有引号。结果应该是

{
"cols": [{
    "label": "id",
    "type": "string"
},
{
    "label": "Q1",
    "type": "number"
},
{
    "label": "Q2",
    "type": "number"
}],
"rows": [{
    "c": [{
        "v": "8710058770"
    },
    {
        "v": 35,
        "f": 40
    }]
},
{
    "c": [{
        "v": "8710058770"
    },
    {
        "v": 60,
        "f": 70
    }]
},
{
    "c": [{
        "v": "8710058770"
    },
    {
        "v": 75,
        "f": 85
    }]
}]

}

因为您的密钥没有包含在引号中,所以这是一个错误。

现在的问题是为什么它不在报价中,而它应该在报价中

  • 请提供print_r($table),然后我可以检查问题所在