2

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?

5
  • 2
    Why not just use json_decode() straight on the file's contents? What's with the other code? Commented Oct 27, 2011 at 10:12
  • It's because of the unicode characters in name (Małopolskie/Kraków/Kraków-Śródmieście). json_last_error() will most likely return JSON_ERROR_UTF8 . Not an answer to the question, but hopes this will help Commented Oct 27, 2011 at 10:13
  • @Darhazer, isn't that a value you are referencing to? Commented Oct 27, 2011 at 10:19
  • Please, after fix your code with the correct variable $new=(json_decode($str, true)); explain us if it's correct or is a charset issue. Commented Oct 27, 2011 at 10:22
  • Simplest thing have done the work, str replacing was totally unnecessar. Thank you for feedback. Commented Oct 27, 2011 at 10:41

4 Answers 4

10
+50

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I've changed my -1 to a +1 since you corrected your answer, but you should make it clear that @PietrC was using a wrong variable.
after spending 2.5 hours this is the best answer, non utf-8 characters were breaking json_decode, and var_dump was showing NULL.
4

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

1 Comment

Thank you for reply, strangly it was the only thing, that i hadn't tried.
2

Use this :

$new = json_decode($str , true);

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.