简体   繁体   English

将JSON从PHP返回到JavaScript?

[英]Returning JSON from PHP to JavaScript?

I have a PHP script that's being called through jQuery AJAX. 我有一个PHP脚本,通过jQuery AJAX调用。 I want the PHP script to return the data in JSON format to the javascript. 我希望PHP脚本将JSON格式的数据返回给javascript。 Here's the pseudo code in the PHP script: 这是PHP脚本中的伪代码:

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

Basically, I need the results of the two for loops to be inserted in $json. 基本上,我需要在$ json中插入两个for循环的结果。

Php has an inbuilt JSON Serialising function. Php具有内置的JSON Serialising功能。

json_encode

json_encode json_encode

Please use that if you can and don't suffer Not Invented Here syndrome. 如果你可以并且没有遭受Not Invented Here综合症, 使用它。

Here are a couple of things missing in the previous answers: 以下答案中缺少以下几点:

  1. Set header in your PHP: 在PHP中设置标题:

     header('Content-type: application/json'); echo json_encode($array); 
  2. json_encode() can return a JavaScript array instead of JavaScript object , see: json_encode()可以返回JavaScript 数组而不是JavaScript 对象 ,请参阅:
    Returning JSON from a PHP Script 从PHP脚本返回JSON
    This could be important to know in some cases as arrays and objects are not the same. 在某些情况下,这可能很重要,因为数组和对象不一样。

There's a JSON section in the PHP's documentation. PHP的文档中有一个JSON部分 You'll need PHP 5.2.0 though. 你需要PHP 5.2.0。

As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default. 从PHP 5.2.0开始,JSON扩展默认捆绑并编译为PHP。

If you don't, here's the PECL library you can install. 如果不这样做,这里是您可以安装的PECL库

<?php
    $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

    echo json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5}
?>

Usually you would be interested in also having some structure to your data in the receiving end: 通常,您会对接收端的数据结构感兴趣:

json_encode($result)

This will preserve the array keys as well. 这也将保留数组键。

Do remember that json_encode only works on utf8 -encoded data. 请记住,json_encode仅适用于utf8编码的数据。

You can use Simple JSON for PHP . 您可以使用Simple JSON for PHP It sends the headers help you to forge the JSON. 它发送头文件可以帮助你伪造JSON。

It looks like : 看起来像 :

<?php
// Include the json class
include('includes/json.php');

// Then create the PHP-Json Object to suits your needs

// Set a variable ; var name = {}
$Json = new json('var', 'name'); 
// Fire a callback ; callback({});
$Json = new json('callback', 'name'); 
// Just send a raw JSON ; {}
$Json = new json();

// Build data
$object = new stdClass();
$object->test = 'OK';
$arraytest = array('1','2','3');
$jsonOnly = '{"Hello" : "darling"}';

// Add some content
$Json->add('width', '565px');
$Json->add('You are logged IN');
$Json->add('An_Object', $object);
$Json->add("An_Array",$arraytest);
$Json->add("A_Json",$jsonOnly);

// Finally, send the JSON.

$Json->send();
?>

$msg="You Enter Wrong Username OR Password"; $ msg =“您输入错误的用户名或密码”; $responso=json_encode($msg); $ responso = json_encode($ MSG);

echo "{\"status\" : \"400\", \"responce\" : \"603\", \"message\" : \"You Enter Wrong Username OR Password\", \"feed\":".str_replace("<p>","",$responso). "}";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM