I have this json file and I converted it in json object:
我有这个json文件,我把它转换成json对象:
[
{
"id": "1",
"nome": "erwrw",
"cognome": "sdsfdfs",
"CF": "qwert",
"eta": "27",
"sesso": "uomo",
"indirizzo": "qwerrt",
"luogo": "wewrw",
"provincia": "ewrewrw",
"citta": "erwrwr",
"comune": "ewrewrw"
},
{
"id": "2",
"nome": "mario",
"cognome": "rossi",
"CF": "MRSI4343242",
"eta": "35",
"sesso": "uomo",
"indirizzo": "via rossi 10",
"luogo": "bergamo",
"provincia": "bergamo",
"citta": "bergamo",
"comune": "bergamo"
}
]
With ajax I can get and print all the keys and values in a html file:
使用ajax,我可以在html文件中获取并打印所有的键和值:
function getData() {
var container = $('div.container');
console.log("container",container);
$.ajax({
type: 'GET',
url: 'data/persona1.json',
dataType: 'json',
success:function(data){
console.log(data);
console.log(data[0].nome);
$.each(data, function(index, item) {
$.each(item, function(key, value){
container.append(key + ' : ' + value + '<br/>');
});
container.append('<br/><br/>');
});
},
});
};
BUT How can I do to print just "nome", "cognome", "citta" of every object?
但是我怎么能只打印“nome”、“cognome”、“citta”的每个对象呢?
5 个解决方案
#1
3
If you know the names of the properties you want to retrieve you can access them directly by name without the loop, try this:
如果您知道要检索的属性的名称,您可以不通过循环直接通过名称访问它们,请尝试以下操作:
var data = [{
"id": "1",
"nome": "erwrw",
"cognome": "sdsfdfs",
"CF": "qwert",
"eta": "27",
"sesso": "uomo",
"indirizzo": "qwerrt",
"luogo": "wewrw",
"provincia": "ewrewrw",
"citta": "erwrwr",
"comune": "ewrewrw"
}, {
"id": "2",
"nome": "mario",
"cognome": "rossi",
"CF": "MRSI4343242",
"eta": "35",
"sesso": "uomo",
"indirizzo": "via rossi 10",
"luogo": "bergamo",
"provincia": "bergamo",
"citta": "bergamo",
"comune": "bergamo"
}]
var container = $('div.container');
$.each(data, function(index, item) {
container.append('Nome : ' + item.nome + '<br/>Cognome : ' + item.cognome + '<br/>Citta : ' + item.citte + '<br/><br/><br/>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="container"></div>
#2
0
Remove the inner each
loop. Then print the keys you want:
删除内部每个循环。然后打印你想要的密钥:
$.each(data, function(index, item) {
container.append('nome : ' + item['nome']+ '<br/>');
container.append('cognome : ' + item['cognome']+ '<br/>');
container.append('citta: ' + item['citta']+ '<br/>');
container.append('<br/><br/>');
});
#3
0
Just put an if condition in your $.each()
在$.each()中输入if条件
var data = [
{
"id": "1",
"nome": "erwrw",
"cognome": "sdsfdfs",
"CF": "qwert",
"eta": "27",
"sesso": "uomo",
"indirizzo": "qwerrt",
"luogo": "wewrw",
"provincia": "ewrewrw",
"citta": "erwrwr",
"comune": "ewrewrw"
},
{
"id": "2",
"nome": "mario",
"cognome": "rossi",
"CF": "MRSI4343242",
"eta": "35",
"sesso": "uomo",
"indirizzo": "via rossi 10",
"luogo": "bergamo",
"provincia": "bergamo",
"citta": "bergamo",
"comune": "bergamo"
}
]
$.each(data, function(index, item) {
console.log(item)
$.each(item, function(key, value){
if(key == "nome" || key == "cognome" || key == "citta")
$('.container').append(key + ' : ' + value + '<br/>');
});
$('.container').append('<br/><br/>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container"></div>
#4
0
The fastest way - create an array with keys you need and check in the loop if key is in the array.
最快的方法——用你需要的键创建一个数组,如果键在数组中,就在循环中进行检查。
fields = ['nome','cognome','citta'];
...
if ($.inArray(key, fields)){
...
}
This way it will be a lot easier to modify the fields than by adding if/else or each name separately.
这样,修改字段要比分别添加if/else或每个名称容易得多。
#5
0
$.each
is very slow. if you have a lot of data you can use for loop
for more performance.
美元。每个速度非常慢。如果您有很多数据,您可以使用for循环获得更多的性能。
link with test performance : link
链接到测试性能:链接
var data = [{
"id": "1",
"nome": "erwrw",
"cognome": "sdsfdfs",
"CF": "qwert",
"eta": "27",
"sesso": "uomo",
"indirizzo": "qwerrt",
"luogo": "wewrw",
"provincia": "ewrewrw",
"citta": "erwrwr",
"comune": "ewrewrw"
}, {
"id": "2",
"nome": "mario",
"cognome": "rossi",
"CF": "MRSI4343242",
"eta": "35",
"sesso": "uomo",
"indirizzo": "via rossi 10",
"luogo": "bergamo",
"provincia": "bergamo",
"citta": "bergamo",
"comune": "bergamo"
}]
var container = $('div.container');
for(var t=0;t<data.length;t++){
container.append('Nome : ' + data[t].nome + '<br/>Cognome : ' + data[t].cognome + '<br/>Citta : ' + data[t].citte + '<br/><br/><br/>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="container"></div>
#1
3
If you know the names of the properties you want to retrieve you can access them directly by name without the loop, try this:
如果您知道要检索的属性的名称,您可以不通过循环直接通过名称访问它们,请尝试以下操作:
var data = [{
"id": "1",
"nome": "erwrw",
"cognome": "sdsfdfs",
"CF": "qwert",
"eta": "27",
"sesso": "uomo",
"indirizzo": "qwerrt",
"luogo": "wewrw",
"provincia": "ewrewrw",
"citta": "erwrwr",
"comune": "ewrewrw"
}, {
"id": "2",
"nome": "mario",
"cognome": "rossi",
"CF": "MRSI4343242",
"eta": "35",
"sesso": "uomo",
"indirizzo": "via rossi 10",
"luogo": "bergamo",
"provincia": "bergamo",
"citta": "bergamo",
"comune": "bergamo"
}]
var container = $('div.container');
$.each(data, function(index, item) {
container.append('Nome : ' + item.nome + '<br/>Cognome : ' + item.cognome + '<br/>Citta : ' + item.citte + '<br/><br/><br/>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="container"></div>
#2
0
Remove the inner each
loop. Then print the keys you want:
删除内部每个循环。然后打印你想要的密钥:
$.each(data, function(index, item) {
container.append('nome : ' + item['nome']+ '<br/>');
container.append('cognome : ' + item['cognome']+ '<br/>');
container.append('citta: ' + item['citta']+ '<br/>');
container.append('<br/><br/>');
});
#3
0
Just put an if condition in your $.each()
在$.each()中输入if条件
var data = [
{
"id": "1",
"nome": "erwrw",
"cognome": "sdsfdfs",
"CF": "qwert",
"eta": "27",
"sesso": "uomo",
"indirizzo": "qwerrt",
"luogo": "wewrw",
"provincia": "ewrewrw",
"citta": "erwrwr",
"comune": "ewrewrw"
},
{
"id": "2",
"nome": "mario",
"cognome": "rossi",
"CF": "MRSI4343242",
"eta": "35",
"sesso": "uomo",
"indirizzo": "via rossi 10",
"luogo": "bergamo",
"provincia": "bergamo",
"citta": "bergamo",
"comune": "bergamo"
}
]
$.each(data, function(index, item) {
console.log(item)
$.each(item, function(key, value){
if(key == "nome" || key == "cognome" || key == "citta")
$('.container').append(key + ' : ' + value + '<br/>');
});
$('.container').append('<br/><br/>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container"></div>
#4
0
The fastest way - create an array with keys you need and check in the loop if key is in the array.
最快的方法——用你需要的键创建一个数组,如果键在数组中,就在循环中进行检查。
fields = ['nome','cognome','citta'];
...
if ($.inArray(key, fields)){
...
}
This way it will be a lot easier to modify the fields than by adding if/else or each name separately.
这样,修改字段要比分别添加if/else或每个名称容易得多。
#5
0
$.each
is very slow. if you have a lot of data you can use for loop
for more performance.
美元。每个速度非常慢。如果您有很多数据,您可以使用for循环获得更多的性能。
link with test performance : link
链接到测试性能:链接
var data = [{
"id": "1",
"nome": "erwrw",
"cognome": "sdsfdfs",
"CF": "qwert",
"eta": "27",
"sesso": "uomo",
"indirizzo": "qwerrt",
"luogo": "wewrw",
"provincia": "ewrewrw",
"citta": "erwrwr",
"comune": "ewrewrw"
}, {
"id": "2",
"nome": "mario",
"cognome": "rossi",
"CF": "MRSI4343242",
"eta": "35",
"sesso": "uomo",
"indirizzo": "via rossi 10",
"luogo": "bergamo",
"provincia": "bergamo",
"citta": "bergamo",
"comune": "bergamo"
}]
var container = $('div.container');
for(var t=0;t<data.length;t++){
container.append('Nome : ' + data[t].nome + '<br/>Cognome : ' + data[t].cognome + '<br/>Citta : ' + data[t].citte + '<br/><br/><br/>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="container"></div>