简体   繁体   English

我如何在我的 json 文件函数中访问结果 [0]?

[英]How can i acess results[0] in my json file function?

i am using a link to generating 3 different results, i am trying to access things in results[0], results[1] or results[2] in my "contactos" function but i don´t know how to do it.我正在使用链接来生成 3 个不同的结果,我试图在“contactos”函数中访问结果 [0]、结果 [1] 或结果 [2] 中的内容,但我不知道该怎么做。 I thought it maybe has to be inside a For cicle, but it doesn´t work either.我认为它可能必须在 For cicle 内,但它也不起作用。 Can someone help me?有人能帮我吗? (every other selector works fine, but when trying to use results[] it does not work) thanks in advance. (每个其他选择器都可以正常工作,但是在尝试使用 results[] 时它不起作用)提前致谢。

 getResultado(); function getResultado() { fetch("https://randomuser.me/api/?results=3") .then(function(response) { return response.json(); }).then(function(json) { for (var i =0; i<json.results.length; i++) { console.log(json.results[i]); objeto = json.results[i]; document.getElementById("json").appendChild(contactos(objeto)); } }); } function contactos(info){ let contactosElement = document.createElement("div"); contactosElement.classList.add("contactos"); let contactoElement = document.createElement("div"); contactoElement.classList.add("contacto"); contactosElement.appendChild(contactoElement); let imagemElement = document.createElement("div"); imagemElement.classList.add("imagem"); contactoElement.appendChild(imagemElement); let textoElement = document.createElement("div"); textoElement.classList.add("texto"); contactoElement.appendChild(textoElement); let tituloElement = document.createElement("h3"); tituloElement.innerText = "Geral"; textoElement.appendChild(tituloElement); let nomeElement = document.createElement("p"); nomeElement.innerText = "Nome: " + info.name.first; textoElement.appendChild(nomeElement); let mailElement = document.createElement("p"); console.log(this.results[0].email); mailElement.innerText = "Email: " + info.results[0].email; textoElement.appendChild(mailElement); let telefoneElement = document.createElement("p"); telefoneElement.innerText = "Telefone: " + info.phone; textoElement.appendChild(telefoneElement); let fotografiaElement = document.createElement("img"); fotografiaElement.setAttribute('src', info.picture.large); imagemElement.appendChild(fotografiaElement); return contactosElement; }

This is a re-write of my answer to fully accommodate what you had in mind.这是我的答案的重写,以完全适应您的想法。 The central point is that you need to loop over the elements of the returned json.results array.中心点是您需要遍历返回的json.results数组的元素。 I did this with json.results.map() , picked out the properties of interest for each user u and join() -ed them into an HTML string which I then placed in the #json div of your page.我用json.results.map()做这json.results.map() ,挑选出每个用户感兴趣的属性ujoin()它们编辑成一个 HTML 字符串,然后我把它放在你页面的#json div 中。

 getResultado(); function getResultado() { fetch("https://randomuser.me/api/?results=3") .then(r=>r.json()) .then(json=>{ document.getElementById("json").innerHTML= '<div "class=contacts">'+ json.results.map(u=> `<div class="contacto"> <div class="imagem"> <img src="${u.picture.large}"> </div> <div class="texto"> <h3>Geral</h3> <p>${u.name.first} ${u.name.last}</p> <p>${u.phone}</p> <p>${u.email}</p> </div> </div>`).join(''); }); }
 <h2>Contactos</h2> <div id="json"></div>

I used the ES6 templating syntax to generate the new HTML string.我使用 ES6 模板语法来生成新的 HTML 字符串。 This is easier to handle (and faster) than your original approach using document.createElement() and element.appendChild() .这比使用document.createElement()element.appendChild()原始方法更容易处理(也更快element.appendChild()

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

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