2013-03-05 18 views
7

Questo fa parte del JSON che ottengo da Foursquare.JQuery ottiene i dati dall'array JSON

JSON

tips: { 
    count: 2, 
    groups: [ 
    { 
     type: "others", 
     name: "Tips from others", 
     count: 2, 
     items: [ 
     { 
      id: "4e53cf1e7d8b8e9188e20f00", 
      createdAt: 1314115358, 
      text: "najjači fitness centar u gradu", 
      canonicalUrl: "https://foursquare.com/item/4e53cf1e7d8b8e9188e20f00", 
      likes: { 
       count: 2, 
       groups: [ 
       { 
        type: "others", 
        count: 2, 
        items: [] 
       }], 
       summary: "2 likes" 
      }, 
      like: false, 
      logView: true, 
      todo: { 
       count: 0 
      }, 
      user: { 
       id: "12855147", 
       firstName: "Damir", 
       lastName: "P.", 
       gender: "male", 
       photo: { 
        prefix: "https://irs1.4sqi.net/img/user/", 
        suffix: "/AYJWDN42LMGGD2QE.jpg" 
       } 
      } 
     }, 
     { 
      id: "4e549e39152098912f227203", 
      createdAt: 1314168377, 
      text: "ajd da vidimo hocu li znati ponoviti", 
      canonicalUrl: "https://foursquare.com/item/4e549e39152098912f227203", 
      likes: { 
       count: 0, 
       groups: [] 
      }, 
      like: false, 
      logView: true, 
      todo: { 
       count: 0 
      }, 
      user: { 
       id: "12855147", 
       firstName: "Damir", 
       lastName: "P.", 
       gender: "male", 
       photo: { 
        prefix: "https://irs1.4sqi.net/img/user/", 
        suffix: "/AYJWDN42LMGGD2QE.jpg" 
       } 
      } 
     }] 
    }] 
} 

ho bisogno di ottenere l'ultimo suggerimento testo, l'utente chi lo ha scritto e la data quando scrisse/post-it.

utente: Damir P.

Data: 1314115358

Testo: najjači forma fisica centar u gradu

ho provato con JQuery e questo funziona per andare a prendere un non -array value:

$.getJSON(url, function(data){ 
    var text= data.response.venue.tips.groups.items.text; 
    alert(text); 
}); 

Ma non funziona con gli array.

Risultato: Unchaught TypeError: Impossibile leggere il 'testo' di proprietà di undefined.

Inoltre, ho provato con $ .each, ma senza alcun effetto.

$.getJSON(url, function(data){ 
    $.each(data.response.venue.tips.groups.items.text, function (index, value) { 
     console.log(value); 
    }); 
}); 

Cosa sto sbagliando?

+0

@JanDvorak Lui è qui per imparare. Non c'è bisogno di fiammeggiare. I gruppi – Johan

+0

sono anche un array. – SteveP

+0

@Johan lo ammetto, è stato formulato male. Tutto ciò che volevo era puntarlo verso le risorse giuste, però. –

risposta

8

È necessario scorrere entrambi i gruppi e gli elementi. $.each() accetta una raccolta come primo parametro e data.response.venue.tips.groups.items.texttenta di puntare a una stringa. Entrambi groups e items sono matrici.

versione verbose:

$.getJSON(url, function (data) { 

    // Iterate the groups first. 
    $.each(data.response.venue.tips.groups, function (index, value) { 

     // Get the items 
     var items = this.items; // Here 'this' points to a 'group' in 'groups' 

     // Iterate through items. 
     $.each(items, function() { 
      console.log(this.text); // Here 'this' points to an 'item' in 'items' 
     }); 
    }); 
}); 

O più semplicemente:

$.getJSON(url, function (data) { 
    $.each(data.response.venue.tips.groups, function (index, value) { 
     $.each(this.items, function() { 
      console.log(this.text); 
     }); 
    }); 
}); 

Nella JSON specificato, l'ultima uno sarebbe:

$.getJSON(url, function (data) { 
    // Get the 'items' from the first group. 
    var items = data.response.venue.tips.groups[0].items; 

    // Find the last index and the last item. 
    var lastIndex = items.length - 1; 
    var lastItem = items[lastIndex]; 

    console.log("User: " + lastItem.user.firstName + " " + lastItem.user.lastName); 
    console.log("Date: " + lastItem.createdAt); 
    console.log("Text: " + lastItem.text); 
}); 

Questo darebbe:

User: Damir P.
Date: 1314168377
Text: ajd da vidimo hocu li znati ponoviti

+0

Eccellente. Questo ottiene tutti i dati ma come ottenere l'ultimo? – Vucko

+0

@Vucko Che cos'è * l'ultimo *? =) –

+1

In questo caso, il testo ** najjači fitness centar u laurea ** è l'ultimo. Quindi è l'ultimo 'item [0] .text' o' item [1] .text'? E come affrontarlo con il tuo codice? – Vucko

6

Non si esegue il loop degli articoli. Prova a modificare:

$.getJSON(url, function(data){ 
    $.each(data.response.venue.tips.groups.items, function (index, value) { 
     console.log(this.text); 
    }); 
}); 
0

Penso che avete bisogno di qualcosa di simile:

var text= data.response.venue.tips.groups[0].items[1].text; 
0

provare questo

$.getJSON(url, function(data){ 
    $.each(data.response.venue.tips.groups.items, function (index, value) { 
     console.log(this.text); 
    }); 
}); 
+0

ciao @satish possiamo usare 'console.log (this.text);' fuori ognuno creando una matrice? – SagarPPanchal

Problemi correlati