0

I am trying to read a JSON from PHP like this:

    [{
  "titulo": "DontAsk",
  "pais": "Austria",
  "country_iso": "AT",
  "direccion": "Mag. Th. Langmann Gmbh Landstrasse 4",
  "cp_ciudad": "A-2000 STOCKERAU",
  "lat": "48.385583",
  "long": "16.207823",
  "telefono": "43-2266-72554-11",
  "fax": "43-2266-72554-44",
  "web": "www.aaa.com"
}, {
  "titulo": "Other One",
  "pais": "Czech Republic",
  "country_iso": "CZ",
  "direccion": "Pod Cihelnou 6",
  "cp_ciudad": "664 161 00 PRAHA 6",
  "lat": "50.092605",
  "long": "14.312707",
  "telefono": "420 233 313 578",
  "fax": "420 233 313 582",
  "web": "www.bbb.com"
}]

The JSON has no errors, I tried with JsonLint and found it clean. I have more insertions, but I only putted 2.

Then I try a code line this:

        $json = json_decode(file_get_contents($url), true);
    var_dump($json);

The URL returns retrieves the URL well, but in the var_dump returns NULL

I saw a lot of answers and questions, and have not found answer. Some help?

Read Json -> Convert in array in PHP -> Retrieve the array

Thanks

yoko
  • 113
  • 1
  • 1
  • 6

2 Answers2

0

The JSON string ($json) in the above code is an array of objects. That is, the outer level is an array literal whose elements are object literals. By default the result of json_decode will be a numerically indexed array of objects

$json = '[{
"titulo": "DontAsk",
"pais": "Austria",
"country_iso": "AT",
"direccion": "Mag. Th. Langmann Gmbh Landstrasse 4",
"cp_ciudad": "A-2000 STOCKERAU",
"lat": "48.385583",
"long": "16.207823",
"telefono": "43-2266-72554-11",
"fax": "43-2266-72554-44",
"web": "www.aaa.com"
}, {
"titulo": "Other One",
"pais": "Czech Republic",
"country_iso": "CZ",
"direccion": "Pod Cihelnou 6",
"cp_ciudad": "664 161 00 PRAHA 6",
"lat": "50.092605",
"long": "14.312707",
"telefono": "420 233 313 578",
"fax": "420 233 313 582",
"web": "www.bbb.com"
}]';
$data = json_decode($json);
echo $data[1]->titulo; 

and for your refference check this http://www.dyn-web.com/tutorials/php-js/json/decode.php

0

If what you want is an associative array for later work with PHP instead of an array of objects (which is what json_decode hands by default), then specify it on the second parameter of the function like this:

$json = '[{
"titulo": "DontAsk",
"pais": "Austria",
"country_iso": "AT",
"direccion": "Mag. Th. Langmann Gmbh Landstrasse 4",
"cp_ciudad": "A-2000 STOCKERAU",
"lat": "48.385583",
"long": "16.207823",
"telefono": "43-2266-72554-11",
"fax": "43-2266-72554-44",
"web": "www.aaa.com"
}, {
"titulo": "Other One",
"pais": "Czech Republic",
"country_iso": "CZ",
"direccion": "Pod Cihelnou 6",
"cp_ciudad": "664 161 00 PRAHA 6",
"lat": "50.092605",
"long": "14.312707",
"telefono": "420 233 313 578",
"fax": "420 233 313 582",
"web": "www.bbb.com"
}]';

$data = json_decode($json, true);
var_dump($data['titulo']);

But if the problem is that it is returning NULL, check your script is getting the expected input at all (e. g. you get a string parseable by json_decode).