将JSON从PHP返回到JavaScript? [英] Returning JSON from PHP to JavaScript?

查看:118
本文介绍了将JSON从PHP返回到JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过jQuery AJAX调用的PHP脚本。我希望PHP脚本将JSON格式的数据返回给javascript。这是PHP脚本中的伪代码:

I have a PHP script that's being called through jQuery AJAX. I want the PHP script to return the data in JSON format to the javascript. Here's the pseudo code in the PHP script:

$json = "{";
foreach($result as $addr)
{
    foreach($addr as $line)
    {
        $json .= $line . "\n";
    }
    $json .= "\n\n";
}
$json .= "}";

基本上,我需要在$ json中插入两个for循环的结果。

Basically, I need the results of the two for loops to be inserted in $json.

推荐答案

Php具有内置的JSON序列化功能。

Php has an inbuilt JSON Serialising function.

json_encode

json_encode

如果可以,请不要使用Not Invented Here综合症。

Please use that if you can and don't suffer Not Invented Here syndrome.

这篇关于将JSON从PHP返回到JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆