【问题标题】:PHP Loop through JSON String from POST requestPHP 循环通过来自 POST 请求的 JSON 字符串
【发布时间】:2017-04-21 08:24:40
【问题描述】:

如何在 PHP 中遍历这个 JSON 字符串并获取所有 URL?

{
    "item": "1.2.840.113619.2.55.1.1762903756.1942.1319006898.442",
    "title": [{
            "images": "{'1.2.840.113619.2.55.1.1762903756.1942.1319006898.446.1':{'url': 'https:www.example11.com', 'observationTime': 'None'}}"
        },
        {
            "images": "{'1.2.840.113619.2.55.1.1762903756.1942.1319006898.446.1':{'url': 'https:www.example33.com', 'observationTime': 'None'}, '1.2.840.113619.2.55.1.1762903756.1942.1319006898.446.55':{'url': 'https:www.example44.com', 'observationTime': 'None'}, '1.2.840.113619.2.55.1.1762903756.1942.1319006898.446.99':{'url': 'https:www.example55.com', 'observationTime': 'None'}}"
        },
        {
            "images": "{'1.2.840.113619.2.55.1.1762903756.1942.1319006898.446.1':{'url': 'https:www.example66.com', 'observationTime': 'None'}}"
        }
    ]
}

尝试了以下方法,但没有成功

$data=json_decode ($stringBody,TRUE);
for($i=0; $i<count($data['title']); $i++) {
  $data=$data['title'];

   foreach($data as $obj){
     $imagesData =$obj['images'];

      foreach($imagesData as $value){
          print "<p>" . $value->url  . "</p>";
      }

   }
}

感谢您的帮助。

【问题讨论】:

  • 您应该为循环中的变量使用不同的名称;不要覆盖$data。另请注意,您不需要循环中的循环,1应该这样做。
  • 当您在 json decode 中传递 true 时,它​​将转换为递归多维数组 .. 所以使用 $value['url'] 而不是 $value-&gt;url
  • @jeroen 谢谢,尝试了您的建议,但没有成功。 foreach($imagesData as $value){ print "

    " . $价值->网址。 "

    "; } 永远不会被调用

标签: php arrays json post


【解决方案1】:
$array_data = json_decode($json_data, true)["title"];
for ($i = 0; $i < count($array_data); $i++) {
    $image = $array_data[$i]['images'];
    //As the content in image is not  a valid json
    // Convert to valid json
    $image_valid_json = str_replace("'", "\"", $image);
    //change to array
    $image_data = json_decode($image_valid_json, true);
    //get the long key
    $key = array_keys($image_data);
    // Changes as per your question
    for ($j = 0; $j < count($key); $j++) {
        $url = $image_data[$key[$j]]['url'];
        echo $url . "\n";
    }
}

您的 json 图像中的数据只是一个字符串,它不是在 jsonlint 验证的有效 json。所以在使用之前需要先转成json,然后才能解码成数组并访问url。

【讨论】:

  • 这看起来很有希望。我得到了一些 URL,而不是全部。我认为这条线 $url = $image_data[$key[0]]['awsurl'];只显示每张图片的第一个 URL
  • @ErnestAppiah 你所有的网址都是一样的,改变它你会看到所有的网址都被打印出来了
【解决方案2】:

试试这个 PHP 代码

$data=json_decode ($stringBody,TRUE);
foreach($data['title'] as $url){
   $string = "[" . str_replace("'", '"', $url['images']) . "]";
   $json = json_decode($string,true);
   foreach($json[0] as $key => $val){
     print_r($json[0][$key]['url']);
     echo "<br/>";
   }
}

【讨论】:

    【解决方案3】:
    $data=json_decode ($stringBody,TRUE);
    for($i=0; $i<count($data['title']); $i++) {
      $data=$data['title'];
    
       foreach($data as $obj){
         $imagesData =$obj['images'];
    
          foreach($imagesData as $value){
              print "<p>" . $value['url']  . "</p>";
          }
    
       }
    }
    

    【讨论】:

    • 仔细查看代码,特别是$data=$data['title'];
    • 谢谢,但是,最后一个 innder for 循环永远不会被调用。它不起作用
    • 用 foreach 代替 for
    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 2015-04-22
    • 2019-02-06
    • 1970-01-01
    • 2021-12-17
    相关资源
    最近更新 更多