javascript - 从字符串中获取对象,其中字符串包含数组中的对象
问题描述
以下是我从服务器获取的数据:
let data = aa[0].innerHTML.trim();
数据:
[
{
"userid" : "3455535",
"roll_number" : "8845",
"Attributes" : {
},
"status" : "Fail",
"barCode" : '5774777858'
}
]
这个数据是typeof string
我想从这个字符串中提取整个对象,我尝试使用 JSON.parse 但它的抛出错误。
console.log(JSON.parse(data))
SyntaxError: Unexpected token ' in JSON at position 326
我也试过(用 '' 替换 [ & ] 然后修剪,但这也不起作用)
console.log(data.replace(/[|]/, ''))
让我知道如何提取数据。
解决方案
您需要在 JSON 响应中'
替换所有出现的 。"
data = data.replace(/\'/g, '"')
推荐阅读
- java - Parent POM vs Super POM - 他们是一样的吗?
- node.js - 使用 mongoDB 在 mongoose 中获取空数组
- reactjs - 使用 React 从 WebAPI JSON 数据创建列表?地图不是函数错误
- c# - 从 C# 调用时无法识别 mysqldump 命令
- playwright - 相当于 cy.scrollTo('bottom') 的剧作家
- ios - 如何将 UICollectionViewDiffableDataSource reorderHandlers 与自定义组合布局一起使用?
- mips - 我不知道如何在 MIPS 编程中处理数组
- arrays - 如何使用powershell将字符串变量拆分为两个数组对象变量?
- python - 向 GCP IoT Core 发送状态和事件后,paho mqtt python 客户端经常断开连接并出现错误代码 7“连接丢失”
- c# - 如何匹配另一个对象的旋转增量?