2

I am trying to decode a json file with php, but it's not displaying anything. The code I have so far:

PHP

$str = file_get_contents('llt_stops.json');
$json = json_decode($str, true);
print_r($json, true);

JSON

{
  "stops": [
    {
      "id": "1",
      "name": "Andreaskyrkan",
      "locId": "740037195"
    },
    {
      "id": "2",
      "name": "Ankarkronan",
      "locId": "740037329"
    },
    {
      "id": "3",
      "name": "Arcushallen",
      "locId": "740037262"
    }
]
}

I've also tried with a foreach loop, but it doesn't display anything either.

foreach ($json['stops'] as $field => $value) {
// Use $field and $value here
}

Update: I get this error, don't know whats wrong with the loop:

Invalid argument supplied for foreach()
Kim Andersson
  • 251
  • 2
  • 4
  • 15

2 Answers2

0

Use

print_r($json);

With true it returns the value, does not print it.

0

It works now. It was the swedish characters in the json file that were not correctly displayed. I changed them from ? to å ä ö, and it worked.

Kim Andersson
  • 251
  • 2
  • 4
  • 15