jQuery - $ .each循环遍历json代码

时间:2022-08-24 00:05:50

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?

由于我的服务器端脚本输出的方式,我收到多个JSON对象。 {jsonhere} {jsonhere1} {jsonhere2} {jsonhere3}等等。他们没有被任何东西分开。如果我做分裂} {我会失去那些括号。那么有一个外环我可以放在常规的$ .each循环上来实现这个功能吗?

Thank you,

Ice

6 个解决方案

#1


2  

Rough algorithm:

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

#2


1  

this isn't JSON.

这不是JSON。

jQuery interprets JSON the lazy way, calling eval() and hoping there's no 'real' code in there; therefore it won't accept that. (i guess you already know this).

jQuery以懒惰的方式解释JSON,调用eval()并希望那里没有“真正的”代码;因此它不会接受。 (我猜你已经知道了)。

it seems your only way out is to treat it as a text string, and use regular expressions to extract the data.

似乎唯一的出路是将其视为文本字符串,并使用正则表达式来提取数据。

#3


1  

The following would be a valid JSON response:

以下是有效的JSON响应:

[
     {JSON},
     {JSON},
     {JSON},
     {JSON}
]

Since your JSON is malformed, simply fix it on the server side. The following code is a bit more brief than what EndangeredMassa suggested and it avoids adding a comma in between braces enclosed in quotes. I'm not so good at RegEx to figure it out with a single .replace().

由于您的JSON格式不正确,只需将其修复到服务器端即可。下面的代码比EndangeredMassa建议的更简单,它避免在引号括起来的大括号之间添加逗号。我不太擅长RegEx用一个.replace()来解决它。

var string = "{\"key\":\"val}{ue\"}{'key':'val}{ue'}{ \"asdf\" : 500 }";
var result = string.match(/('.*?')|(".*?")|(\d+)|({)|(:)|(})/g);
var newstring = "";
for (var i in result) {
    var next = parseInt(i) + 1;
    if (next <= result.length) {
        if (result[i] == "}" && result[next] == "{") {
            newstring += "},";
        }
        else {
    newstring += result[i];
    }
}

}

To loop through the JSON objects use the following:

要循环访问JSON对象,请使用以下命令:

$.each(eval(newstring), function() {
     //code that uses the JSON values
     alert(this.value1);
     alert(this.value2);
});

#4


0  

Unless you can guarantee that any strings in the data will not contain "}{", you can't even safely split it without parsing the JSON at least enough to keep track of whether you are in a string or not. For example if you just split this:

除非你能保证数据中的任何字符串都不包含“} {”,否则你甚至不能安全地拆分它而不解析JSON至少足以跟踪你是否在字符串中。例如,如果您只是拆分它:

{"foo": "}{", "bar": 42}

around "}{", you would end get two invalid JSON objects instead of one.

在“} {”附近,你会得到两个无效的JSON对象而不是一个。

If you know that can never happen, you could split around the braces and append "}" to each element except the last one, and prepend "{" to the last element. I would only do that if there was absolutely no other way though, because it's really fragile.

如果您知道永远不会发生这种情况,您可以拆分大括号并将“}”附加到除最后一个元素之外的每个元素,并将“{”添加到最后一个元素。如果没有其他办法,我只会这样做,因为它真的很脆弱。

#5


0  

I managed to cobble together a working example! Save the following text as an html page. It appears to work well in IE7 and FF3. Let me know if you have any trouble with it.

我设法凑齐了一个工作的例子!将以下文本另存为html页面。它似乎在IE7和FF3中运行良好。如果您有任何问题,请告诉我。

(I used jquery, but it's really not necessary at all.)

(我使用了jquery,但根本没有必要。)

<html>
<head>
    <script type="text/javascript" src="jquery-1.2.6.js"></script>
    <script type="text/javascript">

        function handleClick() {
            var jsonStrs = parse();
            var jsonObjs = [];
            for(var j=0;j<jsonStrs.length;j++) jsonObjs.push( parseJSON(jsonStrs[j]) );

            //jsonObjs now contains an array of json objects 

            document.getElementById('log').innerHTML = '';
            displayResults(jsonObjs);
        }

        function displayResults(jsonObjs) {
            for(var k=0; k<jsonObjs.length; k++) {
                ShowObjProperties(jsonObjs[k]);
            }
        }

        function ShowObjProperties(obj) {
            var property, propCollection = "";

            for(property in obj) {
                propCollection += (property + ": " + obj[property] + "<br>");
            }

            log(propCollection);
        }


        function parseJSON(str) {
            var x_result = null;
            eval('x_result = ' + str);
            return x_result;
        }

        function parse() {
            //Setup
            var out = $('#output');
            var rawinput = $('#inputtext').val();
            var input = rawinput.split('');
            var stack = [];
            stack.top = function() {
                if (this.length == 0) return null;
                return this[this.length-1];
            }
            var jsonStrs = [];

            //Main Loop
            var ch = '';
            var top = '';
            var cursor = 0;
            var i = 0;
            while (i<input.length) {
                //Current Character
                ch = input[i];

                top = stack.top(); 

                if(top == "'" || top == '"') { //Ignore the rest of the string
                    //You can add validation for possible unsafe javascript inside a string, here.

                    ch = input[++i];

                    while(ch != top) {
                        i++;
                        if(i>=input.length) {
                            alert('malformed string');
                            break;
                        }
                        ch = input[i];
                    }
                    stack.pop();

                } else {
                    //You can add validation for unsafe javascript here.

                    if(ch == ' ') {
                        i++;
                        continue; // Ignore spaces
                    }

                    if(ch == "{" || ch == "'" || ch == '"') stack.push(ch);
                    if(ch == "}") {
                        if(top=="{") {
                            stack.pop();
                        } else {
                            alert('malformed string');
                            break;
                        }

                        if(stack.length == 0) {
                            var str = rawinput.substring(cursor, i+1)
                            jsonStrs.push(str);
                            cursor = i+1;
                        }
                    }
                }

                i++;
            }

            return jsonStrs;
        }

        function log(msg) {
            document.getElementById('log').innerHTML += msg + '<br>';
        }

    </script>
</head>

<body>
    <textarea id="inputtext" rows="5" cols="40" style="overflow:auto">{foo:'bar'}</textarea><br>
    <button id="btnParse" onclick="handleClick();">Parse!</button><br /><br />

    <div id="output">
    </div>

    <b>Results:</b>
    <div id="log"></div>

</body>
</html>

#6


0  

This is not JSON, on the client side just fix your JSON code and you can 'eval' it safely.

这不是JSON,在客户端只修复你的JSON代码,你可以安全地“评估”它。

var jsonWrong = '{a:1}{a:2}{a:3}';
var jsonRight = jsonWrong.replace('}{', '},{');
var json = eval('('+jsonRight+')');

#1


2  

Rough algorithm:

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

#2


1  

this isn't JSON.

这不是JSON。

jQuery interprets JSON the lazy way, calling eval() and hoping there's no 'real' code in there; therefore it won't accept that. (i guess you already know this).

jQuery以懒惰的方式解释JSON,调用eval()并希望那里没有“真正的”代码;因此它不会接受。 (我猜你已经知道了)。

it seems your only way out is to treat it as a text string, and use regular expressions to extract the data.

似乎唯一的出路是将其视为文本字符串,并使用正则表达式来提取数据。

#3


1  

The following would be a valid JSON response:

以下是有效的JSON响应:

[
     {JSON},
     {JSON},
     {JSON},
     {JSON}
]

Since your JSON is malformed, simply fix it on the server side. The following code is a bit more brief than what EndangeredMassa suggested and it avoids adding a comma in between braces enclosed in quotes. I'm not so good at RegEx to figure it out with a single .replace().

由于您的JSON格式不正确,只需将其修复到服务器端即可。下面的代码比EndangeredMassa建议的更简单,它避免在引号括起来的大括号之间添加逗号。我不太擅长RegEx用一个.replace()来解决它。

var string = "{\"key\":\"val}{ue\"}{'key':'val}{ue'}{ \"asdf\" : 500 }";
var result = string.match(/('.*?')|(".*?")|(\d+)|({)|(:)|(})/g);
var newstring = "";
for (var i in result) {
    var next = parseInt(i) + 1;
    if (next <= result.length) {
        if (result[i] == "}" && result[next] == "{") {
            newstring += "},";
        }
        else {
    newstring += result[i];
    }
}

}

To loop through the JSON objects use the following:

要循环访问JSON对象,请使用以下命令:

$.each(eval(newstring), function() {
     //code that uses the JSON values
     alert(this.value1);
     alert(this.value2);
});

#4


0  

Unless you can guarantee that any strings in the data will not contain "}{", you can't even safely split it without parsing the JSON at least enough to keep track of whether you are in a string or not. For example if you just split this:

除非你能保证数据中的任何字符串都不包含“} {”,否则你甚至不能安全地拆分它而不解析JSON至少足以跟踪你是否在字符串中。例如,如果您只是拆分它:

{"foo": "}{", "bar": 42}

around "}{", you would end get two invalid JSON objects instead of one.

在“} {”附近,你会得到两个无效的JSON对象而不是一个。

If you know that can never happen, you could split around the braces and append "}" to each element except the last one, and prepend "{" to the last element. I would only do that if there was absolutely no other way though, because it's really fragile.

如果您知道永远不会发生这种情况,您可以拆分大括号并将“}”附加到除最后一个元素之外的每个元素,并将“{”添加到最后一个元素。如果没有其他办法,我只会这样做,因为它真的很脆弱。

#5


0  

I managed to cobble together a working example! Save the following text as an html page. It appears to work well in IE7 and FF3. Let me know if you have any trouble with it.

我设法凑齐了一个工作的例子!将以下文本另存为html页面。它似乎在IE7和FF3中运行良好。如果您有任何问题,请告诉我。

(I used jquery, but it's really not necessary at all.)

(我使用了jquery,但根本没有必要。)

<html>
<head>
    <script type="text/javascript" src="jquery-1.2.6.js"></script>
    <script type="text/javascript">

        function handleClick() {
            var jsonStrs = parse();
            var jsonObjs = [];
            for(var j=0;j<jsonStrs.length;j++) jsonObjs.push( parseJSON(jsonStrs[j]) );

            //jsonObjs now contains an array of json objects 

            document.getElementById('log').innerHTML = '';
            displayResults(jsonObjs);
        }

        function displayResults(jsonObjs) {
            for(var k=0; k<jsonObjs.length; k++) {
                ShowObjProperties(jsonObjs[k]);
            }
        }

        function ShowObjProperties(obj) {
            var property, propCollection = "";

            for(property in obj) {
                propCollection += (property + ": " + obj[property] + "<br>");
            }

            log(propCollection);
        }


        function parseJSON(str) {
            var x_result = null;
            eval('x_result = ' + str);
            return x_result;
        }

        function parse() {
            //Setup
            var out = $('#output');
            var rawinput = $('#inputtext').val();
            var input = rawinput.split('');
            var stack = [];
            stack.top = function() {
                if (this.length == 0) return null;
                return this[this.length-1];
            }
            var jsonStrs = [];

            //Main Loop
            var ch = '';
            var top = '';
            var cursor = 0;
            var i = 0;
            while (i<input.length) {
                //Current Character
                ch = input[i];

                top = stack.top(); 

                if(top == "'" || top == '"') { //Ignore the rest of the string
                    //You can add validation for possible unsafe javascript inside a string, here.

                    ch = input[++i];

                    while(ch != top) {
                        i++;
                        if(i>=input.length) {
                            alert('malformed string');
                            break;
                        }
                        ch = input[i];
                    }
                    stack.pop();

                } else {
                    //You can add validation for unsafe javascript here.

                    if(ch == ' ') {
                        i++;
                        continue; // Ignore spaces
                    }

                    if(ch == "{" || ch == "'" || ch == '"') stack.push(ch);
                    if(ch == "}") {
                        if(top=="{") {
                            stack.pop();
                        } else {
                            alert('malformed string');
                            break;
                        }

                        if(stack.length == 0) {
                            var str = rawinput.substring(cursor, i+1)
                            jsonStrs.push(str);
                            cursor = i+1;
                        }
                    }
                }

                i++;
            }

            return jsonStrs;
        }

        function log(msg) {
            document.getElementById('log').innerHTML += msg + '<br>';
        }

    </script>
</head>

<body>
    <textarea id="inputtext" rows="5" cols="40" style="overflow:auto">{foo:'bar'}</textarea><br>
    <button id="btnParse" onclick="handleClick();">Parse!</button><br /><br />

    <div id="output">
    </div>

    <b>Results:</b>
    <div id="log"></div>

</body>
</html>

#6


0  

This is not JSON, on the client side just fix your JSON code and you can 'eval' it safely.

这不是JSON,在客户端只修复你的JSON代码,你可以安全地“评估”它。

var jsonWrong = '{a:1}{a:2}{a:3}';
var jsonRight = jsonWrong.replace('}{', '},{');
var json = eval('('+jsonRight+')');