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

查看:40
本文介绍了将 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 . "
";
    }
    $json .= "

";
}
$json .= "}";

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

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

使用它,如果您可以并且不要遭受非发明在这里"综合症.

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

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

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