K
If you want, I recommend using a JSON Parser to easily create and modify JSON's like this one: http://json.parser.online.fr/ To create a Array in /questions/tagged/json organized by states use their object as follows:{
"estado":
[
{
"nome": "Foo LTDA",
"endereco": "Endereço de SP",
"cep": "12345-000",
"telefone": "(11) 1234-1234",
"site": "www.foo.com.br",
"email": "email@foo.com.br"
},
{
"nome": "Foo LTDA",
"endereco": "Endereço de SC",
"cep": "12345-000",
"telefone": "(11) 1234-1234",
"site": "www.foo.com.br",
"email": "email@foo.com.br"
},
{
"nome": "Foo LTDA",
"endereco": "Endereço de RJ",
"cep": "12345-000",
"telefone": "(11) 1234-1234",
"site": "www.foo.com.br",
"email": "email@foo.com.br"
}
]
}
So you would have 3 objects(representatives) in the array estado from your JSON, which was just an example, you can have as many objects as you want in an Array JSON.To print on the div would do the following:First: Store the JSON object in a variable to be able to access and also to identify which are representatives in this way:var representante = {
"estado":
[
{
"nome": "Foo LTDA",
"endereco": "Endereço de SP",
"cep": "12345-000",
"telefone": "(11) 1234-1234",
"site": "www.foo.com.br",
"email": "email@foo.com.br"
},
{
"nome": "Foo LTDA",
"endereco": "Endereço de SC",
"cep": "12345-000",
"telefone": "(11) 1234-1234",
"site": "www.foo.com.br",
"email": "email@foo.com.br"
},
{
"nome": "Foo LTDA",
"endereco": "Endereço de RJ",
"cep": "12345-000",
"telefone": "(11) 1234-1234",
"site": "www.foo.com.br",
"email": "email@foo.com.br"
}
]
}
And then you can use a repeat loop to store whatever content you want in your <div>, accessing representante in this way:var len = representante.estado.length,
aryRepresentantes = [];
for (var i=0; i < len; i++){
var nome = representante.estado[i].nome;
var endereco = representante.estado[i].endereco;
var cep = representante.estado[i].cep;
var telefone = representante.estado[i].telefone;
var site = representante.estado[i].site;
var email = representante.estado[i].email;
var strHTML = "<b>"+nome+"<b>"+
"<br>End.: "+endereco+
"<br>CEP: "+cep+
"<br>Fone: "+telefone+
"<br>Site: "+site+
"<br>E-mail: "+email;
aryRepresentantes.push(strHTML);
}
That way you would have an array of for example 3 states, which would be representante.estado[0] the state of SP, representante.estado[1] the state of SC and representante.estado[2] the state of RJ. You can make an array of states to identify which one or only uses a common repetition loop, but I will explain how to use the array of states:function preencheDados(aryRepresentantes){
var aryUF = ["SP","SC","RJ"];
for (var i=0; i < aryUF.length; i++){
$('#div'+aryUF[i]).html(aryRepresentantes[i]);
}
}
Important:In this way name the ID of your divs to <div id="divSP">,<div id="divSC">,<div id="divRJ"> and automatically the content will go into them when running the following function:preencheDados(aryRepresentantes); //nota que o aryRepresentantes foi declarado logo acima no meu laço de repetição lembra? você tem que passar ele como parâmetro.
As you can see, there depending on how your code is /questions/tagged/html and how is your project you can adapt a little the code I proposed above, according to your need, but the concept is this.