如何将json数据转换为字符串数组并显示文本?

时间:2019-04-02 18:45:41

标签: javascript json fetch

所以我正在使用访存API来获取一些数据。 我已经成功地获取了数据,并可以使用控制台日志显示响应。但是,我想实际使用此数据。 api给我“结果”,“ id”和“总计”。我需要帮助,如何将其转换为字符串数组并实际使用从api生成的信息。

我曾尝试弄乱JSON.Parse,但实际上无法将json数据转换为字符串数组。

function addPost(e){       e.preventDefault();

  let itemname = document.getElementById('itemname').value;
  let body = document.getElementById('body').value;

  fetch('https://cors-anywhere.herokuapp.com/https://www.pathofexile.com/api/trade/search/Standard', {
    method:'POST',

    headers: {
      'Accept': 'application/json, text/plain, */*',
      'Content-type':'application/json'
    },
    body:JSON.stringify({"query": {"status": {"option": "online"}, "name":itemname, "stats": [{"type": "and", "filters": []}]},"sort": {"price": "asc"}})
  })
  .then((res) => res.json())
  .then((data) => console.log(JSON.stringify(data)));
}

1 个答案:

答案 0 :(得分:1)

我认为这就是您想要的->

  var item;

  fetch('https://cors-anywhere.herokuapp.com/https://www.pathofexile.com/api/trade/search/Standard', {
    method:'POST',

    headers: {
      'Accept': 'application/json, text/plain, */*',
      'Content-type':'application/json'
    },
    body:JSON.stringify({"query": {"status": {"option": "online"}, "name":itemname, "stats": [{"type": "and", "filters": []}]},"sort": {"price": "asc"}})
  })
  .then((res) => res.json())
  .then((data) => item = JSON.stringify(data));

JSON.parse(item).result