本文介绍了如何在JQuery中通过JSON代码使用$ .each循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于服务器端脚本的输出方式,我收到了多个JSON对象. {jsonhere}{jsonhere1}{jsonhere2}{jsonhere3} etc..它们之间没有任何分隔.如果我要基于}{进行拆分,则会丢失这些括号.那么,有没有我可以放置在常规$.each循环中的外部循环来实现此功能?

Due to the way my serverside script outputs I receive multiple JSON objects. {jsonhere}{jsonhere1}{jsonhere2}{jsonhere3} etc.. They aren't seperated by anything. If I would do a split based }{ I would lose those brackets. So is there an outerloop I can put over the regular $.each loop to make this work?

谢谢

推荐答案

粗糙算法:

Define a stack
Define an array
LOOP on each character in the string
    IF the top item of the stack is a single or double quote THEN
        LOOP through each character until you find a matching single or double quote, then pop it from the stack.
    ELSE
        IF "{", push onto the stack
        IF "}" THEN
            pop a "{" from the stack if it is on top
            IF the stack is empty THEN //we just finished a full json object
                Throw this json object into an array for later consumption
            END IF
        END IF
        IF single-quote, push onto the stack
        IF double-quote, push onto the stack
    END IF
END LOOP

这篇关于如何在JQuery中通过JSON代码使用$ .each循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:01