0

我早些时候发布了这个问题,但需要澄清很多事情以使每个人都更容易。我正在尝试使用 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 代码的示例,那将是很棒的。在过去的几个小时里,我一直在反复测试不同的解决方案,但似乎没有什么能准确显示我需要的东西:/提前谢谢!

4

6 回答 6

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);
于 2012-10-23T09:24:50.673 回答
0
$array = array(
"key1" => array(
    "innerkey1"=>"innerkeyvalue1",
    "innerkey2"=>"innerkeyvalue2",
    "innerkey3"=>"innerkeyvalue3"
    ),
"key2"=>"value2",
"key3"=>"value3"
);
echo json_encode($array);

在这里查看更多jsone_encode

于 2012-10-23T18:17:00.230 回答
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);
于 2012-10-23T10:37:05.497 回答
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);
?>

这应该适合你。

于 2012-10-23T09:24:51.863 回答
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"
  ...
);

只需更改every {}toarray()和every :to 就=>可以在PHP 语法中得到相同的结果。

于 2012-10-23T09:25:07.517 回答
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);
?>
于 2012-10-23T09:26:18.480 回答