I have got below string values in a variable.
var mystring = '{"img_src":"/english/images/spa.jpg",
"img_title":"Enjoy a refreshing shower at 43,000 feet",
"img_alt":"Enjoy a refreshing shower at 43,000 feet",
"img_height":"326",
"img_width":"980",
"header":"Enjoy a refreshing shower at 43,000 feet",
"subheader":"One of many groundbreaking amenities",
"bg_color":"",
"url_href":"1"},
{"img_src":"/english/images/spa1.jpg",
"img_title":"Enjoy a refreshing shower at 43,000 feet",
"img_alt":"Enjoy a refreshing shower at 43,000 feet",
"img_height":"326",
"img_width":"980",
"header":"Enjoy a refreshing shower at 43,000 feet",
"subheader":"One of many groundbreaking amenities",
"bg_color":"",
"url_href":"1"}'
Now I want to convert this string variable into Array object. So that If I try to access it my function it should behave like an array object. For example.
some code to Convert(mystring) to an Array Object(myArryObject), then I want to pass it to my function like below
$.fn.liveBanner = function(myArryObject,optional_options)
{
//here I want to access my myArryObject as below
alert(myArryObject[0].img_src) //then it should give "/english/images/spa.jpg"
});
Please suggest using JQuery.
EDIT:
I did the changes as suggested by @Deceze, please see below:
var myString = "'"+ $("#RotationImages #mainHidden").attr("value")+"'";
var myArrayobj = jQuery.parseJSON('[' + myString + ']');
alert(myArrayobj.img_src);
the above code is giving below error
Error: uncaught exception: Invalid JSON:'{"img_src":"/english/images/spa.jpg", "img_title":"Enjoy a refreshing shower at 43,000 feet","img_alt":"Enjoy a refreshing shower at 43,000 feet", "img_height":"326","img_width":"980","header":"Enjoy a refreshing shower at 43,000 feet","subheader":"One of many groundbreaking amenities on the Flight","bg_color":"","url_href":"1"}'
As your string only contains the objects inside the array, you have to add the array declaration before you parse it: