douzhang2680 2010-08-29 16:50
浏览 48
已采纳

以什么格式将JSON编码数据推送到网站?

I need to push some JSON data to my website which I would like to read in PHP. What type of file should I make this? A PHP file with the JSON inside of a variable? I understand how to make a text file with JSON encoded data in it, but how do I get this into PHP? Should I use a PHP include with the JSON-encoded data in it assigned to a variable? Or should I read the file from PHP and put the contents into a variable?

  • 写回答

2条回答 默认 最新

  • douba4275 2010-08-29 17:05
    关注

    Save your json string as plain text, then you can use:

    $file = yourfile
    $data = file_get_contents($file);
    $parsed = json_decode($data);
    
    // compacted:
    $parsed = json_decode(file_get_contents($file));
    

    See file_get_contents() and json_decode().

    The advantage of doing this (versus storing it in a PHP file then including it) is that now any program or language that understands JSON can read the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?