Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将输入文件对象序列化为JSON?_Javascript_Json_File - Fatal编程技术网

Javascript 如何将输入文件对象序列化为JSON?

Javascript 如何将输入文件对象序列化为JSON?,javascript,json,file,Javascript,Json,File,我想将HTML输入文件转换为JSON字符串,如下所示: var jsonString = JSON.stringify(file); console.log( file ); console.log( jsonString ); 现在,在我的Firebug中,它记录为: File { size=360195, type="image/jpeg", name="xyz.jpg", mehr...} Object {} 为什么jsonString为空 背

我想将HTML输入文件转换为JSON字符串,如下所示:

var jsonString = JSON.stringify(file);
console.log( file );
console.log( jsonString );
现在,在我的Firebug中,它记录为:

File { size=360195, type="image/jpeg", name="xyz.jpg", mehr...} 
Object {}
为什么
jsonString
为空

背景信息:我想用JSONP将文件引用发送到另一个PHP服务器


附加信息:我只想将文件指针(引用)转换为字符串,通过GET发送。

您必须使用。文件对象不包含文件内容(它只是指向文件的指针,允许您稍后读取)

您可以查看此文件以了解有关此API用法的更多信息

var file = getAFile( );

var success = function ( content ) {
  console.log( JSON.stringify( content ) ); }

var fileReader = new FileReader( );
fileReader.onload = function ( evt ) { success( evt.target.result ) };
fileReader.readAsText( file );

在Chrome、Firefox和Safari中使用
JSON.stringify
无法直接将对象转换为JSON

您可以使用
JSON.stringify将
文件
对象转换为字符串

例:

您还可以将添加到
文件
对象中

例:


注意:使用
POST
HTTP方法向服务器发送
文件
对象。

如果有人仍在寻找解决方案,请参阅另一篇文章和JSFIDLE

JS:

函数getFiles(){ var files=document.getElementById(“myFiles”).files; var myArray=[]; var文件={}; console.log(文件);//查看文件列表 //为文件列表中的每个文件手动创建新文件obj 对于(var i=0;i
HTML:



打开此文件夹文件,当您在内部将json字符串Javascript传递给json对象时,运行节点json.js,从而无需解析它

在json文件->

$('#inp_import_template')[0].files[0]

现在,您的json文件被转换为json对象(Javascript)。

我使用了这个函数,并将其用于图像输入,而不是一个接一个地循环或提取每个键

const fileObject = e.target.files[0];
重要通知

//dont use shorthand for of loop
for (const [key, value] in Object.entries(x)) 
it can't loop through a file object in JS 
请改用此代码


您只需要一个自定义替换程序:

function stringify(obj) {
    const replacer = [];
    for (const key in obj) {
        replacer.push(key);
    }
    return JSON.stringify(obj, replacer);
}

const json = stringify(file);
console.log(file);
console.log(json);
现在你应该看到:

File {name: "xyz.jpg", type: "image/jpeg", size...}
'{"name":"xyz.jpg","type":"image/jpeg","size"...}'

谢谢你的回答。我想将指针发送到另一台服务器。因此,我必须将文件指针转换为字符串。你有什么想法吗?你读过这篇文章吗
evt.target.result
是一个包含文件内容的字符串。出于安全考虑,使用FileReaderAPI是获取它的唯一方法。我认为这不是我项目中的解决方案。出现问题,因为我将使用GET发送字符串。例如,如果文件大小为500 MB,则字符串太长。那么,请不要使用GET发送它?我不知道你想要什么。如果您只需要发布一个文件,请使用JSON而不是JSON,因为前者可以简化文件上载,而无需在RAM中缓冲其内容。您无法通过GET发送文件。POST是这里的方式,我不明白你为什么喜欢使用GET。web服务器永远不会愿意解析500Mo URL。实际上,在Chrome中,你也不能对
文件
对象进行字符串化。是的,现在我在Chrome中测试了
JSON。使用
文件
对象进行字符串化
,但不起作用。但是当我回答这个问题的时候我已经测试过了呃。。。什么?文件API不应转换为字符串。请参阅:@williamle8300您错过了问题的一部分,即他想将
文件
对象作为JSON请求发送到
PHP
服务器,以便他想将
文件
对象字符串化。你放的问题链接,询问另一个案例也接受回答说:(你不能序列化
文件
对象)->(与这个问题相同)。我的答案只是解决将
文件
对象字符串化和序列化同义词的问题。这是否回答了问题?我应该如何从后端的这个文件对象获取文件?
$('#inp_import_template')[0].files[0]
const fileObject = e.target.files[0];
//dont use shorthand for of loop
for (const [key, value] in Object.entries(x)) 
it can't loop through a file object in JS 
const imageObject = {};

for (const key in fileObject) {
    const value = fileObject[key];
    const notFunction = typeof value !== "function";
    notFunction && (imageObject[key] = value);
}

console.log(imageObject) // => should give you a normal JS object now
function stringify(obj) {
    const replacer = [];
    for (const key in obj) {
        replacer.push(key);
    }
    return JSON.stringify(obj, replacer);
}

const json = stringify(file);
console.log(file);
console.log(json);
File {name: "xyz.jpg", type: "image/jpeg", size...}
'{"name":"xyz.jpg","type":"image/jpeg","size"...}'