0

I have some JSON file:

"productsAtributesMaping":[
    {
        "model":"first",
        "params":["0", "1", "2"]
    },
    {
        "model":"second",
        "params":["0", "1", "2", "3", "4"]
    }
]

How to output a params of each object with loop?

4
  • 1
    Server side or client side? Commented May 27, 2014 at 7:30
  • First of all, it is an invalid json.. Commented May 27, 2014 at 7:32
  • it's client side. JSON is vaild, it's a part of them Commented May 27, 2014 at 7:32
  • @Manishankar It's only missing parent braces, so I would assume this is just a property of a larger object. Commented May 27, 2014 at 7:33

2 Answers 2

1

You can use each() like this:

$.each(data.productsAtributesMaping, function(i, val) {
    $.each(val.params, function(x, param) {
        console.log(param);
    });
});

Example fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

This place is unbelievable. Give an answer with a demonstration of it working and still get an unexplained downvote.
0

Use parseJSON and for..in loop:

// fill in `jsonData` variable
var obj = jQuery.parseJSON(jsonData);
for(var i in obj) {
    var item = obj[i];
    console.log(item.model);
    console.log(item.params);
}

1 Comment

The data is already in JSON format, so parseJSON is not required. Also, params will output as a joined string, not iterated over as the OP required.

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.