在JavaScript中打印JSON嵌套对象?
要在JavaScript中打印JSON嵌套对象,请将for循环与JSON.parse()一起使用。以下是代码-
示例
var details = [ { "studentId": 101, "studentName": "John", "countryName": "US", "subjectDetails": "{\"0\":\"JavaScript\",\"1\":\"David\"}" }, { "studentId": 102, "studentName": "Bob", "countryName": "UK", "subjectDetails": "{\"0\":\"Java\",\"1\":\"Carol\"}" }, { "studentId": 103, "studentName": "Mike", "countryName": "AUS", "subjectDetails": "{\"0\":\"MongoDB\",\"1\":\"Adam\"}" } ] for (const detailsObject of details) { const subjectDetailsObject = JSON.parse(detailsObject.subjectDetails); console.log(subjectDetailsObject[0]); }
要运行上述程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo145.js。
输出结果
这将产生以下输出-
PS C:\Users\Amit\JavaScript-code> node demo145.js JavaScript Java MongoDB