javascript - 如何解析 Angular 或javascript中的特定对象?

标签 javascript arrays angularjs json object

我们如何在 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/

相关文章:

javascript - document.write() 是做什么的?

javascript - 为什么 preventDefault() 在 Tab 键按下后不停止焦点更改?

Python:将字符串转换为 float ,将 float 读入二维数组,if/then,行重新排序?

javascript - ng-click中Angularjs指令链接调用功能

javascript - 将所有单选按钮的值打印到文本区域?

JavaScript 函数在 HTML 页面中不起作用

arrays - Scala非连续子数组

arrays - Swift 字符串比较字符

android - 使用 angularjs 和 cordova 上传相机图像

javascript - Angular 旋转器 : functions execution order