如何删除数组中的反斜杠?

时间:2015-04-18 10:46:24

标签: javascript jquery

如何删除下面数组中的反斜杠?虽然即时通讯结束了我得到反斜杠的值我怎么能删除它?

[{"album_desc":"","id":1,"album_title":"","album_creation_date":"2015-04-18T10:42:38.519Z","album_pics":["{\"media_type\":\"image/jpeg\",\"last_modified_date\":\"Sat Apr 18 2015 16:12:31 GMT+0530 (IST)\",\"thumnail_pic_loc\":\"LostFile_JPG_63901.jpg\",\"large_pic_loc\":\"LostFile_JPG_63901.jpg\",\"filter_type\":\"image/jpeg\",\"pic_id\":\"d5bd\"},{\"media_type\":\"image/jpeg\",\"last_modified_date\":\"Sat Apr 18 2015 16:12:31 GMT+0530 (IST)\",\"thumnail_pic_loc\":\"LostFile_JPG_67677.jpg\",\"large_pic_loc\":\"LostFile_JPG_67677.jpg\",\"filter_type\":\"image/jpeg\",\"pic_id\":\"d5bd\"},"]}]

Jquery代码

$('.ImageName').append('{"media_type"'+':"'+ fileItem['_file']['type'] + '",'+'"last_modified_date"'+':"'+ new Date() + '",'+'"thumnail_pic_loc"'+':"'+ fileItem['_file']['name'] + '",'+'"large_pic_loc"'+':"'+ fileItem['_file']['name'] + '",'+'"filter_type"'+':"'+ fileItem['_file']['type'] + '",'+'"pic_id"' +':'+'"d5bd"}'+',');

3 个答案:

答案 0 :(得分:1)

看起来您可能正在使用PHP的json_encode来创建JSON。

“album_pics”数组中的JSON 字符串可能需要使用JSON.parse()解析为JSON 对象

答案 1 :(得分:0)

你可以用空字符串替换反斜杠,假设a是这个字符串:

a = '[{"album_desc":"","id":1,"album_title":"","album_creation_date":"2015-04-18T10:42:38.519Z","album_pics":["{\"media_type\":\"image/jpeg\",\"last_modified_date\":\"Sat Apr 18 2015 16:12:31 GMT+0530 (IST)\",\"thumnail_pic_loc\":\"LostFile_JPG_63901.jpg\",\"large_pic_loc\":\"LostFile_JPG_63901.jpg\",\"filter_type\":\"image/jpeg\",\"pic_id\":\"d5bd\"},{\"media_type\":\"image/jpeg\",\"last_modified_date\":\"Sat Apr 18 2015 16:12:31 GMT+0530 (IST)\",\"thumnail_pic_loc\":\"LostFile_JPG_67677.jpg\",\"large_pic_loc\":\"LostFile_JPG_67677.jpg\",\"filter_type\":\"image/jpeg\",\"pic_id\":\"d5bd\"},"]}]';
console.log(a.replace(new RegExp("\\\\", "g"), ""));

给出:

[{"album_desc":"","id":1,"album_title":"","album_creation_date":"2015-04-18T10:42:38.519Z","album_pics":["{"media_type":"image/jpeg","last_modified_date":"Sat Apr 18 2015 16:12:31 GMT+0530 (IST)","thumnail_pic_loc":"LostFile_JPG_63901.jpg","large_pic_loc":"LostFile_JPG_63901.jpg","filter_type":"image/jpeg","pic_id":"d5bd"},{"media_type":"image/jpeg","last_modified_date":"Sat Apr 18 2015 16:12:31 GMT+0530 (IST)","thumnail_pic_loc":"LostFile_JPG_67677.jpg","large_pic_loc":"LostFile_JPG_67677.jpg","filter_type":"image/jpeg","pic_id":"d5bd"},"]}]

答案 2 :(得分:0)

var str = '[{"album_desc":"","id":1,"album_title":"","album_creation_date":"2015-04-18T10:42:38.519Z","album_pics":["{\"media_type\":\"image/jpeg\",\"last_modified_date\":\"Sat Apr 18 2015 16:12:31 GMT+0530 (IST)\",\"thumnail_pic_loc\":\"LostFile_JPG_63901.jpg\",\"large_pic_loc\":\"LostFile_JPG_63901.jpg\",\"filter_type\":\"image/jpeg\",\"pic_id\":\"d5bd\"},{\"media_type\":\"image/jpeg\",\"last_modified_date\":\"Sat Apr 18 2015 16:12:31 GMT+0530 (IST)\",\"thumnail_pic_loc\":\"LostFile_JPG_67677.jpg\",\"large_pic_loc\":\"LostFile_JPG_67677.jpg\",\"filter_type\":\"image/jpeg\",\"pic_id\":\"d5bd\"},"]}]';
var finalData = str.replace(/\\/g, "");
console.log(finalData);

Remove Backslashes from Json Data in JavaScript

这可能对您有所帮助:)。