2013-07-25 100 views
1

我使用PHP創建了一些似乎無效的JSON數據。我正在嘗試將google API集成到我的代碼中。json_encode返回無效的JSON代碼

<?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)output

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}]}]} 
+3

你能粘貼生成的JSON代碼嗎? – Zim84

+0

'json_encode'不會導致「無效的JSON格式」。 – Jon

+1

看到無效的json會有所幫助,在任何情況下,您都可以通過在文件的開頭添加'header(「Content-type」,「application/json」);'開始。 –

回答

4

你在<?php之前在你的php文件的開始處有一些空的空間,這可能會導致輸出錯過解釋。還要確保你在回顯json之前有

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

+0

我已經嘗試過,但我無法解決它。 – user2614247

0

其實問題是由於字符串值沒有引號。 結果應該是

{ 
"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)然後我可以檢查是什麼問題。
+0

我已經更新了我的print_r($ table)輸出。 – user2614247

+0

當我打印兩個輸出時,我是Json和表(數組)我得到正確的輸出,但是當我只打印json時,我得到一個錯誤。 – user2614247