0

json 形式で適切な出力を取得しようとしていますが、以下の出力は少し乱雑です。次のようになります。 "{"table":"users","operation":"select","username":"inan"}"

どうすれば問題を解決できますか?

ありがとう

サーバー.php

print_r($_POST);

client.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 に答える