javascript - 如果包含反斜杠,则无法读取 json 属性

标签 javascript node.js express

我正在尝试从以下请求访问 json:

var files = req.files.fileData[0]; // This contains multiple file data
// initially it is like [object object]

        for(var i=0; i<files.length; i++){
            var fileDataArr = JSON.stringify(files[i]);
            console.log("**********fileDataArr : "+fileDataArr);
            var attachmentId = fileDataArr.name.attachmentId;
            console.log("id : "+attachmentId);
        }

JSON 结构是我得到的,而控制台 fileDataArr 是:

{"domain":null,"_events":{},"_eventsCount":0,"size":5056,"path":"C:\\Users\\k7000649\\AppData\\Local\\Temp\\8eb4f7b82b5646cef78f9989bb3353b1","name":"{\"fileName\":\"wiproNewIcon.png\",\"fileType\":\"image/png\",\"attachmentId\":\"99c148f3f5c1\",\"restrictedFileSize\":\"25690112 \"}","type":"image/png","hash":false,"lastModifiedDate":"2017-08-22T11:25:03.380Z","_writeStream":{"_writableState":{"objectMode":false,"highWaterMark":16384,"needDrain":false,"ending":true,"ended":true,"finished":true,"decodeStrings":true,"defaultEncoding":"utf8","length":0,"writing":false,"corked":0,"sync":false,"bufferProcessing":false,"writecb":null,"writelen":0,"bufferedRequest":null,"lastBufferedRequest":null,"pendingcb":0,"prefinished":true,"errorEmitted":false,"bufferedRequestCount":0,"corkedRequestsFree":{"next":null,"entry":null}},"writable":false,"domain":null,"_events":{},"_eventsCount":0,"path":"C:\\Users\\k7000649\\AppData\\Local\\Temp\\8eb4f7b82b5646cef78f9989bb3353b1","fd":null,"flags":"w","mode":438,"autoClose":true,"bytesWritten":5056,"closed":true},"length":5056,"filename":"{\"fileName\":\"wiproNewIcon.png\",\"fileType\":\"image/png\",\"attachmentId\":\"99c148f3f5c1\",\"restrictedFileSize\":\"25690112 \"}","mime":"image/png"}

结构有效但反斜杠

我试图解析 JSON 以删除反斜杠,但它会抛出类似 SyntaxError: Unexpected token o 的错误,当我尝试从 JSON 访问任何数据 (fileDataArr.name.attachmentId) 时,它会抛出如下错误:

TypeError: Cannot read property 'attachmentId' of undefined
    at exports.xhr_upload_multi_attachment (D:\Harmony_Trunk\Development\Asset-Editor\build\harmony_development\routes\xhr_upload_attachment.js:122:39)

我尝试使用正则表达式 (/\g/,'') 替换反斜杠,这没问题,但我在 JSON 中有文件路径,因此缺少文件路径。

请建议我解决这个问题。

最佳答案

看起来它已被解析,但它在 name 属性中包含嵌套的 JSON 数据。

请注意,双引号仅在该属性中被转义。这是因为您将对象字符串化,这使得该属性编码了两次。

"name":"{\"fileName\":\"wiproNewIcon.png\",\"fileType\":\"image/png\",\"attachmentId\":\"99c148f3f5c1\",\"restrictedFileSize\":\"25690112 \"}"

所以根本不要字符串化,而是在循环中解析 name 属性:

var parsedName = JSON.parse(files[i].name);
console.log(parsedName.attachmentId);

如果可行,则尝试找出为什么该属性使用与其他属性不同的编码来保存其数据。无论是什么生成您的数据,似乎都在编码一个与其他属性分开的属性。

关于javascript - 如果包含反斜杠,则无法读取 json 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45818547/

相关文章:

javascript - 有没有办法在javascript代码中调用 "benchmark"

javascript - 如何定位 DOM 中的下一个 event.target?

php - 无需 "intermediate"即可交互 NodeJS 和 PHP(如 redis)

javascript - 需要中间件功能的路由

javascript - ExpressJS 使用自定义消息限制正文大小

javascript - Node JS Express 未定义回调

javascript - 如何使用正则表达式进行嵌套匹配?

javascript - dojo 的性能(日志记录、 cometd 、 slider )

node.js - NodeJS 抓取 .ashx 页面

Node.js Running Hello World on Google Cloud Platform报错部署报错