1
I have this following json structure:
"[{
"nome":["fulano 1","fulano 2","fulano 3","fulano 4"],
"site":["sitefulano1.html","sitefulano2.html.html","sitefulano3.html","sitefulano2.html"]}]"
I manage it this way with php and mysql Pdo:
<?php
$valorV1 = count($v1);
for($col = 0; $col <= $valorV1-1; $col++) {
$autores['nome'][] = $v1[$col]['value'];
$autores['site'][] = $v2[$col]['site'];
}
echo json_encode([$autores]);
?>
The problem I want to put the json data on the screen and can’t, someone knows how to fix?
I tried some examples didn’t work:
function trataResposta(requisicaoAjax) {
if(requisicaoAjax.readyState == 4) {
if(requisicaoAjax.status == 200 || requisicaoAjax.status == 304) {
var dados = requisicaoAjax.responseText;
alert(dados);
//tentei =
var jsonObj = JSON.parse(dados);
for (var i=0; i < jsonObj.length; i++){
alert(jsonObj[i]);
}
for (var i=0; i < autores.dados.length; i++) { //vai passar por todos os objetos dentro do array
document.getElementById('insere_aqui').innerHTML = dados[i]["nome"];
document.getElementById('insere_aqui').innerHTML = dados[i]["site"];
}
}
}
}
When I tried this way the example works but with my json coming from the bank does nothing!
var Ex01 = {"indice":"valor","nome" : "Silas Stoffel"}
for (var index in dados ) {
alert(index + ': ' + Ex01[index]);
}
Another question is how do I do it, in case you want to put a name on the json like that? In the book case, how do I do this with php and mysql?
{ "livro" :{
"titulo" : "Construindo sites com CSS e (X)HTML",
"autor" : "Maurício",
"site" : "http://livrocss.com.br"
}
}
was written Object on the console when I opened the arrow appeared the data inside
Object
nome
:
"fulano1"
site
:
"sitefulano1.html"
__proto__
:
Object
1
:
Object
nome
:
"fulano2"
site
:
"sitefulano2.html"
__proto__
:
Object
2
:
Object
nome
:
"fulano3"
site
:
"sitefulano3.html"
__proto__
:
Object
3
:
Object
nome
:
"fulano4"
site
your second question does not work because you are trying to paste a structure into Alert that only accepts simple string. If you change the function Alert to console.log I believe it works.
– pmargreff
I got was not sure if the right name is this in the Dice or object level
console.log(dados[0].nome); console.log(dados[1].nome); console.log(dados[2].nome);
Ae appeared the data– hyperpixel
Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!
– Sorack