Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php json_encode returning null

Tags:

json

php

null

Array
(
    [sEcho] => 1
    [iTotalRecords] => 7521
    [iTotalDisplayRecords] => 1
    [aaData] => Array
        (
            [0] => Array
                (
                    [0] => Nordic Capital Buys SiC Processing
                    [1] => 2010-06-21/nordic-capital-buys-sic-processing
                    [2] => PEHub Media
                    [3] => Business
                    [4] => completed
                    [5] => Nordic Capital has acquired a 70% stake in SiC Processing AG, a German industrial recycling company, from Frog Capital. No sale price was disclosed.  SiC Processing’s founding family retains a 25% holding, while former lead investor Zouk Ventures retains a 5% stake.

                    [6] => Admin, China, Frog Capital, Germany, Italy, Iyad Omari, Manufacturing, Norway, PEHub Media, Photovoltaic Wafer Manufacturing, Renewable Energy, Semiconductor, United States
                )

        )

)

echo json_encode($myArr);

{"sEcho":"1","iTotalRecords":7521,"iTotalDisplayRecords":"1","aaData":[[" Nordic Capital Buys SiC Processing</a></div>"," 2010-06-21/nordic-capital-buys-sic-processing</div>","PEHub Media","Business","completed",null," Admin, China, Frog Capital, Germany, Italy, Iyad Omari, Manufacturing, Norway, PEHub Media, Photovoltaic Wafer Manufacturing, Renewable Energy, Semiconductor, United States]]}

Note the null in the middle of the string after completed

Why is this, what escape/manipulation do I need to perform in order to encode this?

I have tried, addslashes

like image 919
Lizard Avatar asked Jul 05 '10 15:07

Lizard


People also ask

Why is Json_decode returning NULL?

If json_decode returns null is it because the database. json is not valid. You can fix this by open the database.

What does json_encode return?

Syntax. The json_encode() function can return a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.

What is json_ encode in PHP?

The json_encode() function is used to encode a value to JSON format.

How can I get JSON encoded data in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.


2 Answers

From the manual:

Note that if you try to encode an array containing non-utf values, you'll get null values in the resulting JSON string. You can batch-encode all the elements of an array with the array_map function:

$encodedArray = array_map(utf8_encode, $myArr);
echo json_encode($encodedArray);
like image 121
robjmills Avatar answered Sep 28 '22 08:09

robjmills


Actually it doesn't return null, http://codepad.org/A34KdUf5.

Maybe your PHP version doesn't support json_encode().

like image 21
Luca Matteis Avatar answered Sep 28 '22 09:09

Luca Matteis