Iterate over .name
(or specifiy deeper if necessary), build your data structure (using data
), and then convert it to JSON. Working example: http://jsfiddle.net/tLuPC/
Below is simulating id
if you needed to obtain that as mentioned in your post. Using data-
attribute to store info. Otherwise you can simply just obtain the name
.
var data = [],
jsonData = null;
$('.name').each(function () {
var item = $(this);
data.push({
id: item.data('id'),
name: $('span', item).text()
});
});
jsonData = JSON.stringify(data);
console.log(JSON.stringify(jsonData));