javascript - 通过key获取json数据

标签 javascript arrays json fetch

  const username = 'merMan';

  fetch("./datz.json")
    .then(response => response.text())
    .then((response) => {
        console.log(response);

    })

我的数据响应如下所示,仍然很难简单地获取用户特定的数据。我的响应数据输出如下。我尝试过使用 find,但总是返回 find is not a function,response[username] 也不起作用。

[{"mermAn":{"baseMapId":"459cc334740944d38580455a0a777a24","customBaseMap":"","zoomn":"5","orient":"0","centLon":"-93.69999999999843","centLat":"38.64999999999935"},
{"catWoman":{"baseMapId":"459cc334740944d38580455a0a777a24","customBaseMap":"","zoomn":"5","orient":"0","centLon":"-93.69999999999843","centLat":"38.64999999999935"},
{"Riddler":{"baseMapId":"459cc334740944d38580455a0a777a24","customBaseMap":"","zoomn":"5","orient":"0","centLon":"-93.69999999999843","centLat":"38.64999999999935"}}]

最佳答案

您需要在 response.text() 之后解析响应,例如:

fetch("./datz.json")
    .then(response => response.text())
    .then((response) => {
        try {
          const parsedArray = JSON.parse(response);

          console.log(parsedArray);
        } catch (error) {
          // response could not be parsed
        }
    })

关于javascript - 通过key获取json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60603925/

相关文章:

javascript - Heremaps 交互式 javascript API : maptile load event

python - 如何通过循环将数组值按索引发送到另一个数组?

javascript - json api get请求错误

javascript - 遇到以下代码未定义输出的问题

javascript - 如何在 Angular-js 中旋转柱形图上的 x 轴标签?

javascript - 通过 AppleScript 使用 Javascript 获取图像尺寸

ruby-on-rails - rails : Flatten array in parameter

arrays - 当我尝试启动它时,我的应用程序崩溃了

javascript - 如何返回 $(this)

javascript - 如何检查一个 JSON 对象是否存在于另一个对象中