-1

I am trying to get data from a JSON page: http://www.oref.org.il/WarningMessages/alerts.json

I am getting errors.

How do I get the "data" array from the JSON?

Here is an example of what I am doing:

$homepage = file_get_contents('http://www.oref.org.il/WarningMessages/alerts.json');
$result   = json_decode($result);
$result   = utf8_encode($result); 
print_r($result);
msturdy
  • 10,479
  • 11
  • 41
  • 52
wyd3x
  • 125
  • 2
  • 10

5 Answers5

2

json_encode returns an object (or an associative array if you give the second argument true). To get the data array, you need to use a property accessor:

$homepage = file_get_contents('http://www.oref.org.il/WarningMessages/alerts.json');
$result = json_decode($homepage);
$data = $result->data;
print_r($data);

If you need to encode the $data array in UTF8, use:

$data = array_map('utf8_encode', $result->data);

To get the title, use:

$title = utf8_encode($data->title);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I try that, for check i change "data" to "title" and change print_r to print bcz "title" is string, didn`t show anything... – wyd3x Jul 13 '14 at 17:58
  • You can't use `array_map` on a string, just encode the string directly. – Barmar Jul 13 '14 at 18:01
  • the title didn`t work, i do var_dump to check, and that print " string(0) "" " – wyd3x Jul 13 '14 at 18:05
  • The problem seems to be that the website is not returning valid JSON. It looks like it's actually JSON that has been encoded as multibyte characters. I tried calling `utf8_decode()` before calling `json_decode()`, but that didn't help. – Barmar Jul 13 '14 at 18:16
  • The result of `json_decode` should already be UTF-8, no need for `utf8_encode` here at all. – deceze Jul 14 '14 at 08:03
1

You could do this:

$result = file_get_contents('http://www.oref.org.il/WarningMessages/alerts.json');
$result = json_decode($result);
print_r($result->data);
Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
0

First of all, you are first json_decode($result); but $result has not been declared before this line.

Secondly, the logically order of your code is totally wrong:

$result = file_get_contents('http://www.oref.org.il/WarningMessages/alerts.json');
$result = utf8_encode($result); // encode the data before decoding json

$result = json_decode($result, true); // specify true to get an associated array

print_r($result);
Chris Lam
  • 3,526
  • 13
  • 10
  • nope, didn`t show anything, and i try and change "print_r" to "var_dump" and that show "NULL" – wyd3x Jul 13 '14 at 18:00
  • `file_get_contents` returns JSON, there's no need to call `json_encode`. – Barmar Jul 13 '14 at 18:03
  • @wyd3x `utf8_encode()` fails because the data fetched is not ISO-8859-1. If you know the encoding of the original data, you can use `iconv()` to convert it to utf8. – Chris Lam Jul 13 '14 at 18:08
  • @Barmar that's not true, `file_get_contents` return string. – Chris Lam Jul 13 '14 at 18:09
  • JSON _is_ a string. That website returns a string containing JSON (except see the comment I just added below my answer -- it's not properly formatted JSON). – Barmar Jul 13 '14 at 18:17
0

$result = file_get_contents ("http://www.oref.org.il/WarningMessages/alerts.json");

 $result = iconv('UTF-16', 'UTF-8', $result);
 $json = json_decode($result);
 echo $json->id;
 echo $json->title;
0

Test.json:

{
    "John":{
        "name":"Json"
    },
    "James":{   
        "status":"Active",
        "age":56,
        "count":10,
        "progress":0.0029857,
        "bad":0
    }
}

json.php:

$result= file_get_contents("test.json");
$get_data = json_decode($result, true);

echo $get_data['John']['name'].'<br/>';
echo $get_data['James']['age'];
Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62