If you want, I recommend using a JSON Parser to easily create and modify JSON’s like this one:
JSON Parser Online
To create a Array
in json organized by states use your 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 protected]"
},
{
"nome": "Foo LTDA",
"endereco": "Endereço de SC",
"cep": "12345-000",
"telefone": "(11) 1234-1234",
"site": "www.foo.com.br",
"email": "[email protected]"
},
{
"nome": "Foo LTDA",
"endereco": "Endereço de RJ",
"cep": "12345-000",
"telefone": "(11) 1234-1234",
"site": "www.foo.com.br",
"email": "[email protected]"
}
]
}
So you would have 3 objects(representatives) in the array estado
of your JSON, of which it was just an example, you can have as many objects as you want in a JSON Array.
To print on div would do the following:
First: Store the JSON object in a variable to be able to access and also to identify what they are representative of 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 protected]"
},
{
"nome": "Foo LTDA",
"endereco": "Endereço de SC",
"cep": "12345-000",
"telefone": "(11) 1234-1234",
"site": "www.foo.com.br",
"email": "[email protected]"
},
{
"nome": "Foo LTDA",
"endereco": "Endereço de RJ",
"cep": "12345-000",
"telefone": "(11) 1234-1234",
"site": "www.foo.com.br",
"email": "[email protected]"
}
]
}
And then you can use a loop 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);
}
This 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 is which or just use a common repetition loop, but I’ll explain it as if I were using the States array:
function preencheDados(aryRepresentantes){
var aryUF = ["SP","SC","RJ"];
for (var i=0; i < aryUF.length; i++){
$('#div'+aryUF[i]).html(aryRepresentantes[i]);
}
}
Important:
This way name your Divs ID to <div id="divSP">
,<div id="divSC">
,<div id="divRJ">
and automatically the content will go into them when performing 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 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.
Help us help you by giving an example of how the div would look with the already printed data ;)
– Oralista de Sistemas
Note: Exchange sensitive information from your code for abstract information. You never know what the intentions of whoever reads your code are.
– André Leria
You have the list by state separately, or you need to extract the information from the address string?
– bfavaretto
there are gentlemen example. @André what did you mean? the list is by state, I just don’t know how I would organize it in json.
– Leandro RR
@Leandroruel I only spoke for taking real information from example. Replace with "FOO LTDA" as you did.
– André Leria
edited my reply @Leandroruel.
– Paulo Roberto Rosa