将字符串添加到json数组的项值

时间:2017-10-08 10:15:21

标签: jquery arrays json

我正在尝试列出上传文件夹中的所有图片。

这是我的PHP代码:

public function listfiles()
{
    define('IMAGEPATH', 'uploads/');
    foreach(glob(IMAGEPATH.'*') as $filename){
        $imag[] =  basename($filename);
    }
    echo json_encode($imag);

}

我对我的服务器执行了ajax get请求,这是我的回复:



$.get( "http://test.dev/templates/listfiles", function( data ) {
		  
		  var host = 'http://test.dev/uploads/';
     			$.each(JSON.parse(data), function( key, value ) {
  				var images =[host + value];
  				console.log(data);
          });




我的回复:

["kkkk.jpg","test.jpg","test1.jpg"]

我正在尝试将字符串添加到我的json数组项值

希望输出如下:



      var host  'www.test.com/';
      
      var images = 
      [
      
      host + 'img/logo.png',
      host + 'img/tmp-blocks.jpg',
      host + 'img/tmp-tgl-images.jpg',
      host + 'img/tmp-send-test.jpg',
      host + 'img/tmp-devices.jpg',
      
      ];




2 个答案:

答案 0 :(得分:0)

首先将image定义为数组并将其定义为循环,然后将连接的字符串推送到image数组中。

$.get( "http://test.dev/templates/listfiles", function( data ) {      
    var host = 'http://test.dev/uploads/';
    var images =[];

    $.each(JSON.parse(data), function( key, value ) {
      images.push(host + value);
    });
    console.log(images);

})

答案 1 :(得分:0)

我试图像这样改变我的代码并且它有效:

LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int x;
int y;

switch (uMsg)
{
case WM_MOUSEMOVE:
    x = LOWORD(lParam);
    y = HIWORD(lParam);

    //printf("Mouse Movement: %d, %d\n", x, y);
    break;

case WM_COMMAND:
{
    int wmId = LOWORD(wParam);
    // Parse the menu selections:
    switch (wmId)
    {
    case 105:
        DestroyWindow(hWnd);
        break;
    default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
}
break;
case WM_PAINT:
{
    printf("WM_PAINT message!\n");
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hWnd, &ps);
    // TODO: Add any drawing code that uses hdc here...
    EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}



        define('IMAGEPATH', 'uploads/');
    foreach(glob(IMAGEPATH.'*') as $filename){
        $imag[] =  'http://cimailer.dev/uploads/'.basename($filename);
    }

    echo json_encode($imag,JSON_UNESCAPED_SLASHES);




感谢您的回答