0

我正在尝试以 json 格式获得正确的输出,但下面的输出有点混乱。它应该是这样的: "{"table":"users","operation":"select","username":"inan"}"

我该如何解决我的问题?

谢谢

服务器.php

print_r($_POST);

客户端.php

$data = array('table'=>'users', 'operation'=>'select', 'uid'=>'yoyo');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($curl_handle);

if ($this->request_response_type == 'array')
{
echo $output;
}
else if ($this->request_response_type == 'json')
{
echo json_encode($output);
}
else if ($this->request_response_type == 'xml')
{
    //xml conversion will be done here later. not ready yet.
}

输出:

"Array\n(\n    [table] => users\n    [operation] => select\n    [uid] => yoyo\n)\n"
4

2 回答 2

2

使用print_r打印出来的数组无法解析回变量。

在你的 server.php 中做echo json_encode($_POST);. 然后在你的 client.php

<?php
//...
$output = curl_exec($curl_handle);

// and now you can output it however you like
if ($this->request_response_type == 'array')
{
    //though i don't see why you would want that as output is now useless if someone ought to be using the variable
    $outputVar = json_decode($output); // that parses the string into the variable
    print_r((array)$outputVar);
    // or even better use serialize()
    echo serialize($outputVar);
}
else if ($this->request_response_type == 'json')
{
    echo $output; //this outputs the original json
}
else if ($this->request_response_type == 'xml')
{
    // do your xml magic with $outputVar which is a variable and not a string
    //xml conversion will be done here later. not ready yet.
}
于 2013-02-15T18:09:09.827 回答
-1

看看JSON.stringify

于 2013-02-15T17:44:24.470 回答