简体   繁体   English

在将对象转换为Javascript #EXIF数据中的JSON时出现JSON.stringify问题

[英]JSON.stringify issue on convert an object to a JSON in Javascript #EXIF Data

Object to json is easy but don't know what happening with my code. JSON对象很简单,但是不知道我的代码发生了什么。

So i wanted to read image metadata using PHP ( most important is image created date-time ) but iPhone and other phone do not send this info with image, its working if try to upload using PC. 因此,我想使用PHP读取图像元数据(最重要的是,图像创建日期时间),但是iPhone和其他电话不会将此信息与图像一起发送,如果尝试使用PC进行上传,则可以正常工作。 but if image is compressed then we lost all metadata. 但是如果图像被压缩,那么我们将丢失所有元数据。

So it's not useful to read metadata using php then i decided to read via JavaScript and for that i am using exif.js . 因此,使用php读取元数据并没有用,然后我决定通过JavaScript进行读取,因此我正在使用exif.js。

for multiple upload everything is working fine but when i try to convert object to json, i am getting empty string. 对于多个上传,一切工作正常,但是当我尝试将对象转换为json时,我得到的是空字符串。

Here is my code 这是我的代码

 $('#ServicePhotos').change(function(){ $('#err_msg').html(''); var objects = {}; var lg = this.files.length; for(var i = 0; i < lg; i++){ (function(file){ var file = file; var file_path = file.name; if(file_path){ var startIndex = (file_path.indexOf('\\\\') >= 0 ? file_path.lastIndexOf('\\\\') : file_path.lastIndexOf('/')); var filename = file_path.substring(startIndex); if (filename.indexOf('\\\\') === 0 || filename.indexOf('/') === 0) { filename = filename.substring(1); } //console.log('uploading image ' + filename); } EXIF.getData(file, function() { var exifData = EXIF.pretty(this); if (exifData) { var allMetaData = EXIF.getAllTags(this); var DateTime = EXIF.getTag(this, "DateTime"); objects[filename] = {'created':DateTime}; } }); })(this.files[i]); } console.log(objects); /*showing all data */ var myJSON = JSON.stringify(objects); console.log(myJSON); /* EMPTY ??? */ $('#err_msg').html(myJSON); $('#meta_data').val(myJSON); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.js"></script> <input id="ServicePhotos" type="file" /> <div id="err_msg"></div> 

Now here "var myJSON" is NOT showing json data 现在,这里“ var myJSON”不显示json数据

Here is console.log 这是console.log

在此处输入图片说明

If you can't stringify it probably means your object doesn't have enumerable properties. 如果无法字符串化,则可能意味着您的对象没有枚举属性。 You can test if your object properties are enumerable , if true, you should be able to use JSON.stringify() 您可以测试对象属性是否可枚举 ,如果为true,则应该可以使用JSON.stringify()

From MDN Web Docs ( https://developer.mozilla.org ): MDN Web文档https://developer.mozilla.org ):

" All the other Object instances (including Map, Set, WeakMap, and WeakSet) will have only their enumerable properties serialized. " 所有其他Object实例(包括Map,Set,WeakMap和WeakSet)将仅对其可枚举的属性进行序列化。

From documentation: 从文档:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM