PiotrC
PiotrC

Reputation: 182

Json to php, json_decode returns NULL

I'm doing JSON parser in php for some API that stores informations about estates. I'm getting problem with the parse, since it's returning me NULL vualue instead of an array or object. Simple JSON codes are parsed nicely, but with this:

{"success":true,"totalCount":1,"data":[{"id":996944,"listingId":"2/2089/OMW","mlsId":null,"swoId":null,"sectionName":"ApartmentRental","geoLat":50.06442971278027,"geoLng":19.953176730749647,"country":{"id":34,"name":"Polska","code":"PL"},"location":{"id":42955,"name":"/Małopolskie/Kraków/Kraków-Śródmieście","province":"Małopolskie","locality":"Kraków","quarter":"Kraków-Śródmieście"},"street":{"id":138781,"name":"Hugona Kołłątaja","fullName":"ul. Hugona Kołłątaja"},"foreignStreet":null,"foreignLocation":null,"contractType":"Exclusive","ownershipType":"Mortgage","groundOwnershipType":null,"isSpecial":true,"price":{"amount":2700,"currency":"PLN"},"priceBeforeReduction":null,"dateCreated":"2011-10-22 20:32:35","lastUpdated":"2011-10-25 11:51:09","actualisationDate":"2011-10-22 20:32:34","statusChangeDate":"2011-10-22 20:32:34","images":[{"id":6514430},{"id":6514431},{"id":6514432},{"id":6514433},{"id":6514434},{"id":6514435},{"id":6514436},{"id":6514437},{"id":6514438},{"id":6514439},{"id":6514440},{"id":6514441},{"id":6514442},{"id":6514443}],"licenceNumber":null,"description":"Do wynajęcia piękne, przestronne mieszkanie w wysokim standardzie, świeżo po remoncie przy ulicy Hugona Kołłątaja w Krakowie. Znajduje się w bardzo pięknej z zewnątrz i wewnątrz kamienicy. Bardzo dobra komunikacja, duża powierzchnia 85m2 i wysoki standard wykończenia dają poczucie komfortu. Bardzo blisko Rynku Głównego, Hali Targowej i Galerii Kazimierz. Dzięki bardzo dobrze rozwiniętej komunikacji miejskiej możliwość szybkiego dostania się w każde miejsce Krakowa. Blisko wiele punktów handlowych i usługowych.\n\nMieszkanie składa się 3 odzielnych pokoi, przedpokoju, kuchni oraz 2 łazienek i balkonu. Jest możlwość umeblowania mieszkania według Państwa preferencji na koszt Właściciela. Ogrzewanie własne samodzielne. Możliwość podłączenia Internetu i telefonu.\n\n","englishDescription":null,"russianDescription":null,"parentListingId":null,"totalArea":85,"priceM2":{"amount":31.76,"currency":"PLN"},"noOfFloors":null,"floorNo":null,"furnished":true,"auctionStartingPrice":null,"auctionFrom":null,"auctionTo":null,"openDaysFrom":null,"openDaysTo":null}]}

i get: NULL

Although online parser like http://json.parser.online.fr/ is doing the work just fine.

I was using php built-in funcion json_decode, and some more from php.net, done by users.

Here's my php code:

    $url="url_to_json";
$str=file_get_contents($url);
$str = substr($str, 1, strlen($str) - 2); 
$str = preg_replace("/([a-zA-Z0-9_]+?):/" , "\"$1\":", $str); 
$new=(json_decode($new, true));
var_dump($new);

Any ideas?

Upvotes: 2

Views: 17791

Answers (4)

Tarak Moyyi
Tarak Moyyi

Reputation: 1

When $json_a = json_decode($string, true); is return the null value so you can use below code:

$json_a = json_decode(utf8_encode($string), true);

Upvotes: -1

Mob
Mob

Reputation: 11098

Please use json_decode(); on the raw JSON data. As long as it is valid JSON you don't need any extra code to be able to access this smoothly.

Access the returned value as an array.

$arr = json_decode(file_get_contents($json));

Upvotes: 4

corretge
corretge

Reputation: 1759

If the same JSON string is parsed in other online parser, you have a character-encoding issue, try to convert the string previously with utf8_encode:

 $new=(json_decode(utf8_encode($str), true));

As explained in json_decode:

The json string being decoded.

This function only works with UTF-8 encoded data.

Upvotes: 10

Riz
Riz

Reputation: 10246

Use this :

$new = json_decode($str , true);

Upvotes: 2

Related Questions