开发者

convert string to object for using jQuery.each() function

开发者 https://www.devze.com 2023-03-29 03:44 出处:网络
I create a string: myOption = "{val1:\'text1\', val2:\'text2\'...}" I need convert this string into a Object:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消