【问题标题】:get data from JSON with php使用 php 从 JSON 获取数据
【发布时间】:2014-07-13 17:47:55
【问题描述】:

我正在尝试从 JSON 页面获取数据: http://www.oref.org.il/WarningMessages/alerts.json

我遇到了错误。

如何从 JSON 中获取“数据”数组?

这是我正在做的一个例子:

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

【问题讨论】:

  • utf8_encode() 的参数应该是一个字符串。 json_encode() 返回一个对象。
  • 那么我需要做什么?你能给我举个例子吗?
  • 摆脱utf8_encode。无论如何都没有理由在这里使用它。
  • 我在希伯来语上使用 utf8_encode bcz 标题,当更新时数组将是希伯来语
  • 你说你只想要数据数组,那么为什么标题很重要?

标签: php json get


【解决方案1】:

json_encode 返回一个对象(如果您给出第二个参数true,则返回一个关联数组)。要获取data 数组,您需要使用属性访问器:

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

如果您需要以 UTF8 编码 $data 数组,请使用:

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

要获取标题,请使用:

$title = utf8_encode($data->title);

【讨论】:

  • 我试试,为了检查,我将“数据”更改为“标题”并将 print_r 更改为打印 bcz“标题”是字符串,没有显示任何内容...
  • 不能对字符串使用array_map,直接对字符串进行编码即可。
  • 标题不起作用,我做 var_dump 来检查,然后打印“string(0)”“”
  • 问题似乎是网站没有返回有效的 JSON。看起来它实际上是被编码为多字节字符的 JSON。我尝试在拨打json_decode() 之前拨打utf8_decode(),但这没有帮助。
  • json_decode的结果应该已经是UTF-8了,这里根本不需要utf8_encode
【解决方案2】:

你可以这样做:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      首先,你是第一个json_decode($result);,但$result在这行之前没有被声明。

      其次,你的代码的逻辑顺序是完全错误的:

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

      【讨论】:

      • 不,没有显示任何内容,我尝试将“print_r”更改为“var_dump”并显示“NULL”
      • file_get_contents 返回 JSON,无需调用 json_encode
      • @wyd3x utf8_encode() 失败,因为获取的数据不是 ISO-8859-1。如果你知道原始数据的编码,可以使用iconv()将其转换为utf8。
      • @Barmar 这不是真的,file_get_contents 返回字符串。
      • JSON 一个字符串。该网站返回一个包含 JSON 的字符串(请参阅我刚刚在答案下方添加的评论——它的 JSON 格式不正确)。
      【解决方案5】:

      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'];
      

      【讨论】:

        猜你喜欢
        • 2015-04-08
        • 1970-01-01
        • 2020-02-22
        • 2016-09-22
        • 1970-01-01
        • 1970-01-01
        • 2023-01-16
        • 2012-09-20
        • 1970-01-01
        相关资源
        最近更新 更多