简体   繁体   English

用php解码json文件

[英]decode json file with php

I am trying to decode a json file with php, but it's not displaying anything. 我正在尝试用php解码json文件,但未显示任何内容。 The code I have so far: 我到目前为止的代码:

PHP PHP

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

JSON 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循环,但是它也不显示任何内容。

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

Use 采用

print_r($json);

With true it returns the value, does not print it. 如果为true,则返回值,不打印。

It works now. 现在可以使用了。 It was the swedish characters in the json file that were not correctly displayed. json文件中的瑞典语字符未正确显示。 I changed them from ? 我把他们从? to å ä ö, and it worked. 到åäö,它奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM