Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php JSON_encode not working

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.

Edit : Sorry. Here is outputs -

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 }

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 )
like image 536
Samith Dilshan Avatar asked Feb 01 '17 04:02

Samith Dilshan


1 Answers

json_encode() function returns "false" when the encode fails and a "false" result does not appear in an echo or print. See: http://php.net/manual/en/function.json-encode.php The best way to handle such problems would be using the json_last_error_msg() method and performing an action according to the found error. See: http://php.net/manual/en/function.json-last-error-msg.php. An example would be:

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

The thing is, if the problem was an encoding character, it will no be shown. Just hope that character is not essential for your work or if you can find the mismatch input in a zigazillion string list, correct it. Other types of errors can be found here: http://php.net/manual/en/json.constants.php. Just apply the if statement and correction for the error you find.

I hope this helps someone.

like image 200
Volfegan Avatar answered Oct 28 '22 16:10

Volfegan