javascript语法如何把json文件输出到html页面上

html语法:

<div id="outputjson"></div>

javascript语法

// 异步读取JSON文件
fetch('/json/data.json')
  .then(response => response.json())
  .then(data => {
    // 将JSON内容转换为HTML并插入到页面中
    document.getElementById('outputjson').innerHTML = '<pre>' + JSON.stringify(data, null, 2) + '</pre>';
  })
  .catch(error => console.error('Error reading JSON file:', error));