开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜