javascript - PHP JSON_encode 不工作

标签 javascript php json api

我想得到 this来自 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);

我找不到原因。有人请帮我找到解决方案。

编辑:抱歉。这是输出-

预期的 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 }

我根本没有得到任何 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 )

最佳答案

感谢@awiebe,我找到了确切的错误。这是

Malformed UTF-8 characters, possibly incorrectly encoded

谢谢大家,我从另一个问题中找到了解决方案。 'Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel

关于javascript - PHP JSON_encode 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41972084/

相关文章:

javascript - 单击一次按钮即可触发按钮

javascript - AngularJS ng-repeat 如何从 jquery 每行添加数据

javascript - HTML 元素上的用户可定义属性?

javascript - 如何使用javascript按tab键访问div?

php - $facebook -> getUser() 返回错误值?

php - jquery fadeToggle 不适用于回显脚本

bash 变量中的 json 字符串

php - 将带有下拉菜单的文本插入数据库

java - 使用自动生成的名称将 java 对象序列化为 json

jquery - Internet Explorer $.ajax/MVC JSON 调用失败