Sam*_*han 3 javascript php api json
我想从this获取此 JSON输出。不幸的是json_encode()函数不会将数组编码为该格式。一切都没有回报。这是我的代码。
$output = array(
'responseData' => array(),
'responseDetails' => null,
'responseStatus' => 200
);
$x = 0;
while ($row = mysqli_fetch_assoc($result)) {
foreach ($row as $k => $v) {
$output['responseData']['result'][$x][$k] = $v;
}
$x++;
}
print_r($output);
header('Content-Type: application/json');
echo json_encode($output , JSON_FORCE_OBJECT);
Run Code Online (Sandbox Code Playgroud)
我找不到原因。有人请帮助我找到解决方案。
编辑:对不起。这是输出-
预期的JSON输出-
{
"responseData": {
"results": [{
"qid": 1,
"qtitle": "When do we finish this project ?",
"qimage_url": "http://www.wearesliit.com/example.png",
"user": "samith",
"date": "2016-01-01T02:15:12.356Z",
"type": 1,
"category": 5,
"tags": ["common_senese", "truth", "bazsa_awsanna"],
"note": "Sample quetion"
}, {}, {}]
},
"responseDetails": null,
"responseStatus": 200 }
Run Code Online (Sandbox Code Playgroud)
我根本没有得到任何JSON输出。但是这是数组的print_r结果。
Array(
[responseData] => Array
(
[result] => Array
(
[0] => Array
(
[question_ID] => 1
[question_Title] => Which shape does not belong with the other three shapes?
[question_Image_URL] => http://www.wearesliit.com/images/quiz/questions/1.jpg
[quetion_Note] => Easy IQ question.
[category_ID] => 7
[username] => samith
[added] => 2017-01-29 21:50:52
)
[1] => Array
(
[question_ID] => 2
[question_Title] => Tim earns $10 per hour at his job. When he gets paid on Friday, he is paid for 40 hours of work. He then goes out and spends 10% of his earnings on entertainment that weekend. How much money is he left with on Monday?
[question_Image_URL] =>
[quetion_Note] => Easy IQ question.
[category_ID] => 7
[username] => samith
[added] => 2017-01-29 21:50:52
)
)
)
[responseDetails] =>
[responseStatus] => 200 )
Run Code Online (Sandbox Code Playgroud)
json_encode() 函数在编码失败并且“false”结果未出现在回显或打印中时返回“false”。请参阅:http : //php.net/manual/en/function.json-encode.php 处理此类问题的最佳方法是使用 json_last_error_msg() 方法并根据发现的错误执行操作。请参阅:http : //php.net/manual/en/function.json-last-error-msg.php。一个例子是:
$show_json = json_encode($output , JSON_FORCE_OBJECT);
if ( json_last_error_msg()=="Malformed UTF-8 characters, possibly incorrectly encoded" ) {
$show_json = json_encode($API_array, JSON_PARTIAL_OUTPUT_ON_ERROR );
}
if ( $show_json !== false ) {
echo($show_json);
} else {
die("json_encode fail: " . json_last_error_msg());
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果问题是编码字符,则不会显示。只是希望该字符对您的工作不是必需的,或者如果您可以在无数字符串列表中找到不匹配的输入,请更正它。其他类型的错误可以在这里找到:http : //php.net/manual/en/json.constants.php。只需应用 if 语句并更正您发现的错误即可。
我希望这可以帮助别人。
| 归档时间: |
|
| 查看次数: |
13460 次 |
| 最近记录: |