php JSON_encode 不起作用 [英] php JSON_encode not working

查看:58
本文介绍了php JSON_encode 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 this 获取 JSON 输出="http://www.wearesliit.com/webapp/modules/quiz/api/read.php" rel="noreferrer">这个.不幸的是 json_encode() 函数不会将数组编码为该格式.根本就没有回来.这是我的代码.`

I want to get this JSON output from this . unfortunately json_encode() function won't encode array into that format. It's nothing returning at all. here is my code.`

$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);

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

I can't find the reason. someone please help me find the solution.

抱歉.这是输出 -

预期的 JSON 输出 -

Expected JSON output -

{
"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 结果.

I don't get any JSON output at all. but here is the print_r result of the array.

 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,我找到了确切的错误.这是

Thanks to @awiebe I found the exact error. It's

格式错误的 UTF-8 字符,可能编码不正确

Malformed UTF-8 characters, possibly incorrectly encoded

谢谢大家,我从另一个问题中找到了解决方案.Laravel 中的格式错误的 UTF-8 字符,可能编码不正确"

Thank you all, I found a solution from an another question. 'Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel

这篇关于php JSON_encode 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆