我正在提交一个JSON,并试图访问各个密钥。
在此期间,我尝试了不同的方法。
const jsonData = target.getAttribute('datas');
console.log(jsonData.id); // returns undefined
const jsonData = target.getAttribute('datas');
console.log(jsonData[0].id); // returns undefined
for(let key of Object.keys(jsonData)) {
console.log(key);
} // returns every single letter
for (const [key, value] of Object.entries(jsonData)) {
console.log(`${key}: ${value}`);
} // returns every single letter闲逛,为什么。我不需要解析json。每个代码解释都像我的一个变体一样。
//编辑
JSON:
{"id":"adf6cfce022e4798b11f3ae6bcd8dc0f","manufacturer":"..ö..","productName":"Blue Boat XL","productNumber":"A80801902","worth":[{"currencyId":"b7d2554b0ce847cd82f3ac9bd1c0dfca","net":11.05,"gross":11.9,"linked":false,"listPrice":null,"percentage":null,"regulationPrice":null,"extensions":[]}]}仍未进入
const jsonData = JSON.parse(target.getAttribute('datas'));
console.log(jsonData.id);//解决方案//双解析LMAO
发布于 2022-05-25 10:46:30
听起来你还没有分析过数据--我猜jsonData实际上是一个字符串。您需要使用JSON.parse将字符串转换为对象。
尝试:
const jsonData = JSON.parse(target.getAttribute('datas'));
console.log(jsonData);。。看看你能得到什么。
https://stackoverflow.com/questions/72376157
复制相似问题