简体   繁体   English

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

[英]How can I convert my json data to a string array and display the text?

So I'm using the fetch api to get some data.所以我使用 fetch api 来获取一些数据。 I have sucessfully managed to get the data and can display the response with console log.我已经成功地获取了数据,并且可以通过控制台日志显示响应。 However, I want to actually use this data.但是,我想实际使用这些数据。 The api gives me "result", "id" and "total". api 给了我“结果”、“id”和“总计”。 I need help how can I convert this to a string array and actually use the information generated from the api.我需要帮助如何将其转换为字符串数组并实际使用从 api 生成的信息。

I have tried messing around with JSON.Parse but I haven't really been able to convert the json data to a string array.我试过搞乱 JSON.Parse 但我还没有真正能够将 json 数据转换为字符串数组。

function addPost(e){ e.preventDefault();函数 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)));
}

I think this is what you are looking for->我想这就是你要找的->

  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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM