我之前发布过这个问题,但需要澄清一些让每个人都更容易的事情。我正试图用JSON输出这个,而我正在做的事似乎没有用......
{
"request": {
"act": "user_create",
"user_email": "newuser@example.com",
"user_zone": "example.com"
},
"result": "success",
"msg": "A message that will be displayed to the end user"
...
}
以下是我目前正在使用的代码:
<?php
$array[0]=array("request"=>array("act"=>'user_create',
"user_email"=>"newuser@example.com",
"user_zone"=>"example.com"));
$array[1]=array('result' => 'success');
$array[2]=array('msg' => 'A message that will be displayed to the end user');
echo json_encode($array);
?>
使用该代码我得到的输出很接近但不完全存在并且不是有效的json输出。不确定要改变什么,但这是我目前得到的:
[{"request":{"act":"user_create","user_email":"newuser@example.com","user_zone":"example.com"}},{"result":"success"},{"msg":"A message that will be displayed to the end user"}]
如果有人可以请发布我的固定PHP代码示例,这将是非常好的。在过去的几个小时里,我一直在反复弹跳测试不同的解决方案,似乎没有任何东西正好显示我需要的东西:/提前致谢!
答案 0 :(得分:2)
试试那个..
$array = array(
"request" => array(
"act"=>"user_create",
"user_email"=>"newuser@example.com",
"user_zone"=>"example.com"
),
"result"=>"success",
"msg"=>"A message that will be displayed to the end user"
);
echo json_encode($array);
答案 1 :(得分:0)
<?php
$array["request"]=array("act"=>'user_create',
"user_email"=>"newuser@example.com",
"user_zone"=>"example.com");
$array["result"] = 'success';
$array["msg"] = 'A message that will be displayed to the end user';
echo json_encode($array);
?>
这应该适合你。
答案 2 :(得分:0)
$array = array(
"request" => array(
"act" => "user_create",
"user_email" => "newuser@example.com",
"user_zone" => "example.com"
),
"result" => "success",
"msg" => "A message that will be displayed to the end user"
...
);
只需将每个{}
更改为array()
,将每个:
更改为=>
,您就可以在PHP语法中获得相同的内容。
答案 3 :(得分:0)
<?php
$array[0]["request"] = array(
"act" => 'user_create',
"user_email" => "newuser@example.com",
"user_zone" => "example.com")
);
$array[0]['result'] = 'success';
$array[0]['msg'] = 'A message that will be displayed to the end user';
... etc. for other requests and results and msgs
echo json_encode($array);
?>
答案 4 :(得分:0)
这很有效。每当以JSON格式启动数组时,在PHP中创建数组。
$array = array( // "request" array
"request" => array( // inner array
"act"=>"user_create",
"user_email"=>"newuser@example.com",
"user_zone"=>"example.com"
),
"result"=>"success",
"msg"=>"A message that will be displayed to the end user"
);
echo json_encode($array);
答案 5 :(得分:0)
$array = array(
"key1" => array(
"innerkey1"=>"innerkeyvalue1",
"innerkey2"=>"innerkeyvalue2",
"innerkey3"=>"innerkeyvalue3"
),
"key2"=>"value2",
"key3"=>"value3"
);
echo json_encode($array);