Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- {"foo": "}{", "bar": 42}
- <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>
- var jsonWrong = '{a:1}{a:2}{a:3}';
- var jsonRight = jsonWrong.replace('}{', '},{');
- var json = eval('('+jsonRight+')');
- [
- {JSON},
- {JSON},
- {JSON},
- {JSON}
- ]
- 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];
- }
- }
- $.each(eval(newstring), function() {
- //code that uses the JSON values
- alert(this.value1);
- alert(this.value2);
- });
Add Comment
Please, Sign In to add comment