是否可以加载在一个目录中找到的多个json文件?类似于*regex,但使用Jquery.或者将json文件合并为一个



这是我一直在解决的一个难题。我似乎想不出最好的方法。

我让我的服务器将多个json文件拉到一个目录中,json文件都在同一结构中。每个文件都有一个随机生成的文件名。

我的最终目标是将每个json文件的某些元素附加到一个易于阅读的html表中。

以下是我当前的代码,它可以很好地与单个json文件配合使用:

$.getJSON('testing1.json, function(data) {
$.each(data.user.products, function () {
$("table").append($("<tr>").append(
$("<td>").addClass("Title").text(this.title),
$("<td>").addClass("Price").text("$"+this.price),
$("<td>").addClass("Stock").text(this.stock),
));
});
});

现在我需要想办法解决我的问题。我有大约250个json文件需要循环使用。显然,这个正则表达式不起作用,但可能会让你知道我要做什么:

$.getJSON('*.json, function(data) { 

我宁愿不必将它们全部合并到一个文件中。但事实上,所有的名称都是随机生成的,而且我不知道如何将多个json文件加载到我的代码中,我担心我将不得不这样做。但如果真是这样,那就不是世界末日。

是否可以使用Jquery/PHP在目录中循环并提取所有json文件?如果是这样的话,如何加载每个json文件来附加数据?

我真的很感激事先的帮助,即使是朝着正确的方向努力也非常感激。

创建一个.php文件并调用该文件。在这个文件中,使用php方法从指定的目录中加载所有文件,比如说从单词json开始。这将允许您在1个请求中加载回所有文件。

就像一个例子一样,这将提供一个很好的起点:

/**
* load all site-wide jscript_*.js files from includes/jscript, alphabetically
*/
$directory_array = $template->get_template_part($template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript'), '/^jscript_/', '.js');
while(list ($key, $value) = each($directory_array)) {
echo '<script type="text/javascript" src="' .  $template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript') . '/' . $value . '"></script>'."n";
}

//加载所有JS文件,主要功能如下:

function get_template_part($page_directory, $template_part, $file_extension = '.php') {
$directory_array = array();
if ($dir = @dir($page_directory)) {
while ($file = $dir->read()) {
if (!is_dir($page_directory . $file)) {
if (substr($file, strrpos($file, '.')) == $file_extension && preg_match($template_part, $file)) {
$directory_array[] = $file;
}
}
}
sort($directory_array);
$dir->close();
}
return $directory_array;
}
function get_template_dir($template_code, $current_template, $current_page, $template_dir, $debug=false) {
//  echo 'template_default/' . $template_dir . '=' . $template_code;
if ($this->file_exists($current_template . $current_page, $template_code)) {
return $current_template . $current_page . '/';
} elseif ($this->file_exists(DIR_WS_TEMPLATES . 'template_default/' . $current_page, preg_replace('///', '', $template_code), $debug)) {
return DIR_WS_TEMPLATES . 'template_default/' . $current_page;
} elseif ($this->file_exists($current_template . $template_dir, preg_replace('///', '', $template_code), $debug)) {
return $current_template . $template_dir;
} else {
return DIR_WS_TEMPLATES . 'template_default/' . $template_dir;
//        return $current_template . $template_dir;
}
}
function file_exists($file_dir, $file_pattern, $debug=false) {
$file_found = false;
$file_pattern = '/'.str_replace("/", "/", $file_pattern).'$/';
if ($mydir = @dir($file_dir)) {
while ($file = $mydir->read()) {
if (preg_match($file_pattern, $file)) {
$file_found = true;
break;
}
}
$mydir->close();
}
return $file_found;
}

使用glob,我能够扫描并合并所有.json文件。虽然不是很理想,但它在大多数情况下都有效。

<?php
$files = glob("*.json");
$newDataArray = [];
foreach($files as $file){
$thisData = file_get_contents($file);
$thisDataArray = json_decode($thisData);
$newDataArray[] = $thisDataArray;
}
$newDataJSON = json_encode($newDataArray);
file_put_contents("merged.json",$newDataJSON);
?>  

最新更新