简体   繁体   中英

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.

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. 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 .

for multiple upload everything is working fine but when i try to convert object to json, i am getting empty string.

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

Here is 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()

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

" All the other Object instances (including Map, Set, WeakMap, and WeakSet) will have only their enumerable properties serialized. "

From documentation:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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