我想从PHP脚本返回JSON。
我只是重复结果吗?我必须设置内容类型头吗?
我想从PHP脚本返回JSON。
我只是重复结果吗?我必须设置内容类型头吗?
当前回答
这是一个简单的PHP脚本,返回男性女性和用户id作为json值将是任何随机值,因为你调用脚本json. PHP。
希望能有所帮助,谢谢
<?php
header("Content-type: application/json");
$myObj=new \stdClass();
$myObj->user_id = rand(0, 10);
$myObj->male = rand(0, 5);
$myObj->female = rand(0, 5);
$myJSON = json_encode($myObj);
echo $myJSON;
?>
其他回答
如果你想要js对象,使用头content-type:
<?php
$data = /** whatever you're serializing **/;
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
如果你只想要json: remove header content-type属性,只需要encode和echo。
<?php
$data = /** whatever you're serializing **/;
echo json_encode($data);
用header(' content -type: application/json')设置内容类型;然后对数据进行回显。
<?php
$data = /** whatever you're serializing **/;
header("Content-type: application/json; charset=utf-8");
echo json_encode($data);
?>
这是一个简单的PHP脚本,返回男性女性和用户id作为json值将是任何随机值,因为你调用脚本json. PHP。
希望能有所帮助,谢谢
<?php
header("Content-type: application/json");
$myObj=new \stdClass();
$myObj->user_id = rand(0, 10);
$myObj->male = rand(0, 5);
$myObj->female = rand(0, 5);
$myJSON = json_encode($myObj);
echo $myJSON;
?>
你问题的答案在这里,
它说。
JSON文本的MIME媒体类型为 application / json。
所以如果你设置标题为那种类型,并输出你的JSON字符串,它应该工作。