dtnpf35197 2015-03-26 14:15
浏览 47

将json对象从php传递给javascript

I have a json array in php and I want to pass it to javascript so I can use it with google charts.

If I do this:

var a = <?php echo((json_encode($data))); ?>;

I get the data in the format Name,Value,PHP,78,JAVA,1000,HTML,129 but I want to keep it in the json format that it was in

[["Name","Value"],["PHP",78],["JAVA",1000],["HTML",129]]​

because google charts needs to receive it like this. Any idea how to do this?

  • 写回答

1条回答 默认 最新

  • dongni8969 2015-03-26 14:18
    关注
    <?php
    $book = array(
        "title" => "JavaScript: The Definitive Guide",
        "author" => "David Flanagan",
        "edition" => 6
    );
    ?>
    <script type="text/javascript">
    var book = <?php echo json_encode($book, JSON_PRETTY_PRINT) ?>;
    /* var book = {
        "title": "JavaScript: The Definitive Guide",
        "author": "David Flanagan",
        "edition": 6
    }; */
    alert(book.title);
    </script>
    

    try it hope this will help...

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)