我们如何在 Angular 或 JavaScript 中解析以下类型的对象,也许使用搜索循环或解析?
我想获取标题值并将其分配给标题,因为正如您所看到的,标题的值是对象:
{'title': 'Hey', 'instruction': 'Take a sad song a…75, 'sub_title': 'Jude', 'timelimit': '01:05:01'}
而不是您在示例中看到的“嘿”(第二个对象也相同)。我们有办法做到这一点吗?
JSON 对象数组格式:
[
{
id:0,
title:"{'title': 'Hey', 'instruction': 'Take a sad song a…75, 'sub_title': 'Jude', 'timelimit': '01:05:01'}"
},
{
id:1,
title:"{'title': 'Assessment', 'instruction': 'Jude', 'cr…71, 'sub_title': 'Test', 'timelimit': '06:25:08'}"
}
]
期望的输出:
[
{
id:0,
title:"Hey"
},
{
id:1,
title:"Assessment"
}
]
最佳答案
确保 json 格式正确 - 内部双引号,外部单引号。
像这样
'{"title": "Hey", "instruction": "Take a sad song a…75", "sub_title": "Jude", "timelimit": "01:05:01"}'
然后你就可以简单地做。
let jsonString = '{"title": "Hey", "instruction": "Take a sad song a…75", "sub_title": "Jude", "timelimit": "01:05:01"}';
let title = JSON.parse(jsonString).title;
console.log(title);
关于javascript - 如何解析 Angular 或javascript中的特定对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55017514/