I have the JSON string below :
{ “name”:”Ruby on Rails Baseball
Jersey”, “price”:”19.99″,
“id”:”1025786064″,
“image”:”http://127.0.0.1:3001/assets/products/4/product/ror_baseball.jpeg”
}, { “name”:”Ruby on Rails Baseball
Jersey”, “price”:”19.99″,
“id”:”1025786064″,
“image”:”http://127.0.0.1:3001/assets/products/5/product/ror_baseball_back.jpeg”
}, { “name”:”Ruby on Rails Ringer
T-Shirt”, “price”:”19.99″,
“id”:”187438981″,
“image”:”http://127.0.0.1:3001/assets/products/9/product/ror_ringer.jpeg”
}, { “name”:”Ruby on Rails Ringer
T-Shirt”, “price”:”19.99″,
“id”:”187438981″,
“image”:”http://127.0.0.1:3001/assets/products/10/product/ror_ringer_back.jpeg”
}, { “name”:”Apache Baseball
Jersey”, “price”:”19.99″,
“id”:”706676762″,
“image”:”http://127.0.0.1:3001/assets/products/1004/product/apache_baseball.png”
}, { “name”:”Ruby Baseball Jersey”,
“price”:”19.99″, “id”:”569012001″,
“image”:”http://127.0.0.1:3001/assets/products/1008/product/ruby_baseball.png”
}
Then in jQuery:
var d = eval("(" + data + ")"); //data is the json string above.
$.each(d, function(idx, item) {
alert(item);
});
There is no error, but it only shows the first sequence’s data. How can I loop through all the data?
Thank you.
Firstly, make sure you use JSON parser from here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
then your code will look somewhat like this:
or in Jquery: