tags:

views:

54

answers:

5

I have a 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);      
});

It's no error,but only show the first one sequence's data,how can i loop thought all the data. thanks you.

+1  A: 

Using eval() to parse JSON is unsafe. Try using the browser-native (no libraries needed!) function JSON.parse() instead, which is implemented across all browsers and is secure.

Delan Azabani
It's not implemented across all browsers. It's not even implemented in IE 8.
David Hedlund
@Delan, You are wrong. Native JSON support has been added in EcmaScript 3.1. Thus IE6 and IE7 and older versions of Firefox don't support it.
rochal
+1  A: 

Crockford advises against using eval to parse JSON, so you should be using the JSON.parse function.

Here is where you would use it:

http://www.json.org/js.html

Camoy
+1  A: 

Try:

var d = eval("[" + data + "]");

JSON arrays are wrapped in square brackets, e.g.: '[ 1,2,3 ]'

(Yes, and don't use eval but the JSON built-in object or similar, safer solutions)

adamk
That would return an array with one member, the JSON content. To avoid that, we use plain old parentheses, which doesn't add any wrapping of any sorts, but rather just prevents syntax errors.
Delan Azabani
@Delan - No, look at the OP's sample string, it's a comma separated list.
adamk
Ah, I see now. Sorry!
Delan Azabani
Yes, you are right, thanks you very much.
SilverNight
+1  A: 

Try wrapping your JSON with [ ]

You can validate your JSON here: JSONLint Validator to get some sense as to where the problem lies.

hydrogen
+1  A: 

Firstly, make sure you use JSON parser from here: http://www.json.org/json2.js

then your code will look somewhat like this:

var myArrayOfObjects = JSON.parse("[" + data + "]");
for (obj in myArrayOfObjects)
{
   alert("Name:" + myArrayOfObjects[obj].name);
}

or in Jquery:

$.each(myArrayOfObjects , function(i, o) {
 alert("Name:" + o.name);      
});
rochal
You are right! i should use "[" + data + "]" instead of "(" + data + ")".
SilverNight