开发者

How i can convert string to array in jquery

hi i have my data in a string like this

Array
(
    [0] => Array
        (
            [pollId] => 2
            [poll] => new
            [cId] => 7
            [communityId] => 7
            [ansId] => Array
                (
        开发者_运维知识库            [0] => 6
                    [1] => 7
                )

            [answer] => Array
                (
                    [0] => new1
                    [1] => new2
                )

            [vote] => Array
                (
                    [0] => 37
                    [1] => 36
                )

            [count] => 2
        )

)

i want to convert it to array.


If you really have a string containing the text as quoted, you're going to have to either find or write a parser for it.

It's not a million miles off JSON, and so you might get a leg up on your implementation (if you have to write one) from the two non-eval implementations of JSON parsers found on Crockford's github page. There's json_parse.js, which is a recursive-descent parser; and json_parse_state.js which is a state machine. In both cases, they parse JSON, not your example format, but my point is that as your example format isn't a million miles away from JSON, you can probably use them as a starting point.

Update: (I'd added a comment about this, but it's better as part of the answer.) I see from the comment stream on your question that you don't have to use this format, but instead can use JSON instead. In that case, you're in good shape — jQuery has JSON parsing built in, in its parseJSON function. That will accept a string containing JSON-encoded data and return a JavaScript object graph (which may or may not be an array, depending on what the JSON defines). If you retrieve the JSON via Ajax, jQuery will even parse it for you automatically and give you the resulting object graph as an argument to your success function, see $.ajax and $.getJSON. Here's a live example I did for another question just earlier today. That loads the JSON found at the URL you'll see in the code, has jQuery automatically parse it, and accesses one of the properties of the resulting JavaScript object graph.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜