如何获得这样的json格式?

问题描述:

我想创建一个像这样的json响应,如下所示。如何通过修改下面的php代码来完成。我尝试了很多方法,但结果并不存在。如何获得这样的json格式?

请找到链接编辑

https://www.froala.com/wysiwyg-editor/docs/concepts/image/manager 这是我从代码获得电流响应低于

[ 
    "http://cloudpanda.org//images/media/01fe5273acbd47e413b02bbcfae5a20ac868d037.jpg", 
    "http://cloudpanda.org//images/media/022fa4051066da105688a8ca0f83d222cef3739d.jpg", 
] 

期望的回应,我从代码需要如下:

[ 
     { 
      "url":"http://cloudpanda.org//images/media/01fe5273acbd47e413b02bbcfae5a20ac868d037.jpg", 
     }, 
     { 
      "url":"http://cloudpanda.org//images/media/022fa4051066da105688a8ca0f83d222cef3739d.jpg", 
     }, 

    ] 

当前的PHP代码如下所示:

<?php 
// Array of image links to return. 
$response = array(); 

// Image types. 
$image_types = array(
    "image/gif", 
    "image/jpg", 
    "image/pjpeg", 
    "image/jpeg", 
    "image/pjpeg", 
    "image/png", 
    "image/x-png" 
); 

// Filenames in the uploads folder. 
$fnames = scandir("images/media/"); 

// Check if folder exists. 
if ($fnames) { 
    // Go through all the filenames in the folder. 
    foreach ($fnames as $name) { 
     // Filename must not be a folder. 
     if (!is_dir($name)) { 
      // Check if file is an image. 
      if (in_array(mime_content_type(getcwd() . "/images/media/" . $name), $image_types)) { 
       // Add to the array of links. 
       array_push($response, "http://cloudpanda.org/images/media/" . $name); 
      } 
     } 
    } 
} 

// Folder does not exist, respond with a JSON to throw error. 
else { 
    $response  = new StdClass; 
    $response->error = "Images folder does not exist!"; 
} 

$response = json_encode($response); 

// Send response. 
echo stripslashes($response); 
?> 
+0

这里有什么您的问题? –

+0

我需要获取响应变量作为上面的json代码。当然,我只是得到一个网址列表只有我需要得到它像这样的“网址”:“https://i.froala.com/assets/photo1.jpg”, –

+0

你的链接是平淡的 –

编辑您这样的代码,更改

array_push($response, "http://cloudpanda.org/images/media/" . $name); 

$response[]['url'] = "http://cloudpanda.org/images/media/" . $name; 
+1

谢谢你哟它工作:) –