unix_user
unix_user

Reputation: 309

Parsing JSON object using jQuery

I am trying to parse the json object using jquery but it is giving me error that cannot get the length of a null object. I am not able to find the reason. Please help me in finding the issue. Here is what i am doing

            var slider_images = {
                "image": [
                            {"fname":"1", "caption":"this is 1"},
                            {"fname":"2", "caption":"this is 2"},
                            {"fname":"3", "caption":"this is 3"},
                            {"fname":"4", "caption":"this is 4"},
                            {"fname":"5", "caption":"this is 5"},
                            {"fname":"6", "caption":"this is 6"},
                            {"fname":"7", "caption":"this is 7"},
                            {"fname":"8", "caption":"this is 8"}
                        ]
            };

            var imageObj = $.parseJSON(slider_images.image);
            $.each(imageObj,function() {
                //alert("fname is::"+this['fname']);
                alert("1");
            });

I want to get the fname and caption for all the subobjects that fall under image. Please let me know where I am going wrong...

Thanks!

Upvotes: 1

Views: 3391

Answers (1)

Flash
Flash

Reputation: 16703

Parsing JSON means converting a string into a JavaScript object. You already have an object so have nothing to 'parse'.

You can just use your object like this:

 $.each(slider_images.image, function() {
        console.log(this);
 });

Upvotes: 4

Related Questions