无法访问JSON对象中的属性

时间:2019-04-03 09:56:30

标签: javascript node.js

  1. 我创建了一个包含一系列属性的记录对象(var record = {...})

  2. console.log(记录)给我:

{ 'Label': 'Test Location 3', 'Doshii Location ID': '123', 'Zip API Key': 'LBUcHlihsel7JkiCuPykiRuQ1+upbiEZuRFYU/cI/VZ7e0u0ldSkJlcD/NvTUZJCkMznQ8Wk4/qPfzfyfopzQcATzkde3H/0EQbwfJ3qWY5GZj8aK2yUNPGP+rLZA3BeQOQhu4iJuZsVdno/prfG7419x78v5P1fwY8AMC6u6nSUW69bjD5dplMqdH7fQ+1gWlN0UIpMzJeY7TugELofk85DdQHxtu7/oeKiSdJ0Qs6Z/FprWEqcyUgKVHwsUBcGyIYzrGZx4Yt/GjanlhYhibPtmHyMQUseZ7x5TJodSlDxjHR8pSa6+MXLQ9xRs8Hn6COr7vVnIGMJtMkkNxmJ/g==', 'Zip Location ID': '123' }

  1. console.log(JSON.stringify(record)); 给我:

{"Label":"Test Location 3","Doshii Location ID":"123","Zip API Key":"LBUcHlihsel7JkiCuPykiRuQ1+upbiEZuRFYU/cI/VZ7e0u0ldSkJlcD/NvTUZJCkMznQ8Wk4/qPfzfyfopzQcATzkde3H/0EQbwfJ3qWY5GZj8aK2yUNPGP+rLZA3BeQOQhu4iJuZsVdno/prfG7419x78v5P1fwY8AMC6u6nSUW69bjD5dplMqdH7fQ+1gWlN0UIpMzJeY7TugELofk85DdQHxtu7/oeKiSdJ0Qs6Z/FprWEqcyUgKVHwsUBcGyIYzrGZx4Yt/GjanlhYhibPtmHyMQUseZ7x5TJodSlDxjHR8pSa6+MXLQ9xRs8Hn6COr7vVnIGMJtMkkNxmJ/g==","Zip Location ID":"123"}

  1. var label = record["Label"];

  2. var newLocationObj = { doshiiLocationId: record["Doshii Location ID"], label: label, zipApiKey: record["Zip API Key"], zipLocationId: record["Zip Location ID"], ownerUserKey: req.body.ownerUserKey, createdByUserKey: tokenResult.userKey, createdDate: currentDate, lastModifiedDate: currentDate };

  3. console.log("new location object - ",newLocationObj);

  4. 输出为:

    新位置对象-{ doshiiLocationId: '123', label: undefined, zipApiKey: 'YlBEMw3YfX7uHiD8N8fjBI/Vq1IDVyjJhd1tN/gHrzIXLQ2iokJhfdx4Xuk8ovn4cFE6ftnxtDK/9WKKhEWtSVJgbBLBDhivLTe6k2y8S4e5x9QP2EQIIOM/psU+CL0ECO6S+UV859KOybI7MQZ4R+3wpTXeRn/k7uHX//Nnto/kqICTzh8ua4gkKRxdG2BnlvU0A54GzC+MTAWvRSAmjHMOupT3PrIEv/vMnpdVqFMNk8e0Ks+o6TOsYS/NEue5OKYRkPhZwhrgJj12Tz5Mphi8+XJcGSRw1X5unTmpG5q6FUF/ekv3Xig4KAvrOPJWImPSZqvIU0V4rG7Y9H+uNw==', zipLocationId: '123', ownerUserKey: 'a791154f-c978-4c4d-8721-d3ee4e910fdf', createdByUserKey: 'a791154f-c978-4c4d-8721-d3ee4e910fdf', createdDate: '2019-04-03T09:53:57.724Z', lastModifiedDate: '2019-04-03T09:53:57.724Z' }

我一生都无法弄清楚为什么标签未定义...有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您的Label键在\ufeffZero Width No-Break Space, Byte Order Mark or BOM)之前包含以下字符。

因此,要使用的右键是\ufeffLabel而不是Label,它将返回undefined

const label = record['\ufeffLabel'];

答案 1 :(得分:0)

正如jo_va所说,您的标签密钥带有'\ ufeff'(字符),因此可以将其删除或只是对对象进行字符串化处理并解析以解决该问题。 enter image description here