一种可能的解决方法是使用jQuery的$.ajax函数。以下是示例代码:
$.ajax({ type: "GET", url: "example.json", dataType: "json", success: function(data){ console.log(data); //输出JSON数据对象 try { var parsedData = JSON.parse(data); console.log(parsedData); //输出解析后的JSON对象 } catch (e) { console.error(e); } }, error: function(jqXHR, textStatus, errorThrown){ console.error(errorThrown); } });
在这个例子中,我们通过jQuery的$.ajax函数获取JSON数据。如果成功获取数据,则调用回调函数,将数据打印到控制台。然后,我们试图使用JSON.parse将数据解析为JavaScript对象。如果解析过程出现错误,则会在控制台中打印错误信息。