PHP从POST请求循环通过JSON字符串

时间:2017-04-21 08:24:41

标签: php arrays json post

如何在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>";
      }

   }
}

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

"com.zaxxer.hikari.HikariJNDIFactory"

json图像中的数据只是一个字符串,并不是jsonlint验证的有效json。因此,在使用它之前,您需要转换为json,然后才能解码到数组并访问URL。

答案 1 :(得分:0)

试试这个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/>";
   }
}

答案 2 :(得分:-1)

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

   }
}