convert string to object for using jQuery.each() function
I create a string:
myOption = "{val1:'text1', val2:'text2'...}"
I need convert this string into a Object:
myOption = {val1:'text1', val2:'text2'开发者_StackOverflow...}
for using the jquery each function and take a pair value/text in each iteration.
Now in each iteration, it takes one char of the string.
You can use:
$.parseJSON(myOption);
Keep in mind that your JSON string needs to be well-formed, and that means double-quoting property names and values:
var myOption = '{"val1":"text1", "val2":"text2"}';
var obj = $.parseJSON(myOption);
$.each(obj, function(i, val) {
alert(i + ' ' + val);
});
Demo.
精彩评论