0

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.

5 Answers 5

1

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:

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);      
});
Sign up to request clarification or add additional context in comments.

1 Comment

You are right! i should use "[" + data + "]" instead of "(" + data + ")".
1

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.

2 Comments

It's not implemented across all browsers. It's not even implemented in IE 8.
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.
1

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

Comments

1

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)

2 Comments

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 - No, look at the OP's sample string, it's a comma separated list.
1

Try wrapping your JSON with [ ]

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.