douxin2003 2017-03-20 08:41
浏览 293

将json文件加载到变量中

I have created a PHP programm which allows to load a random .json file on my computer. When I select the file I want to display, I get the data from the selected .json file. At the beginning, I used a default file which I replaced with a variable. Here is the PHP code :

<?php

   $uploaddir = 'uploads/';
   $uploadfile = $uploaddir . basename($_FILES['file']['name']);
   $file_test = explode('.', $_FILES['file']['name']);
   $file_ext= strtolower(end($file_test));

   $expensions = array(".json");

if(in_array($file_ext,$expensions)=== false){
  $errors[]="extension not allowed, please choose a DBC or an XML file.";
}

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
        echo "<div style='font-size: 150%;'>". "File is valid, and was successfully uploaded.
"."</div>";

    } else {
        echo "<div style='font-size: 150%;'>". "Oops, It seems you haven't uploaded a file ! Please make sure you have selected a .json file
 "."</div> <br>";
        echo "<div style='font-size: 150%;'>". "Returning to Home...
 "."</div>";
        header("Refresh: 3,URL=upload_file.php");
        exit();
}

echo basename($_FILES['file']['name']);

?>

<?php 

  header("Refresh: 2,URL=json_content_extended.php?filename=".$uploadfile);
  exit();

?>

This file contains the code where I declared the requested value :

<?php

$json_file = file_get_contents($_REQUEST["filename"]);
$jfo = json_decode($json_file, true);

?>

And here is the HTML part :

<!DOCTYPE html>
<html>
<head>
    <title>Menu</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
    <form action="upload.php" method="POST" enctype="multipart/form-data" >
        <div class="test">
        <input type="file" accept=".json" name="file"/>
        <button class="up" name="click" class="click">Submit file</button>
        </div>
    </form>
</body>
</html>

This is just an example ! What I actually want to do is the same thing in Javascript. I started the script with a default file, but now I would like to replace this file (default.json) with a variable. I use $.getJson. Here is the code :

$(document).ready(function() {
    var files = ["default.json"];
$.getJSON(files, function(json) {
  var tr0, tr1, tr2, tr3, tr4, tr5, tr6, tr7, tr8, tr9;

    tr0 = $('<tr/>');
    tr0.append("<td>" + '<input class="expand" name="bt0" type="button" value="+" />' + "</td>");  
    tr0.append("<td>" + json.messages[0].id + "</td>");
    tr0.append("<td>" + json.messages[0].name + "</td>");
    tr0.append("<td>" + json.messages[0].is_extended_frame + "</td>");

I wonder if there is somehow a possibility to set a variable instead of the file name. I would like to do the same thing I did in the PHP example, but this time in javascript.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮