Latest: AjaMyJSON.js source listing /*** ** AjaMyAjax --> JSON functions library ** use with the core AJAX lib routines in ajaMyCore.js. ** ** This software provided "AS IS," but is FREE to use and share. ** For details see the as-is doc and Creative Commons license: ** http://creativecommons.org/licenses/by-sa/3.0/ ** ** See doc.txt or examples.html for syntax and more info at ** http://AjaMyAjax.com, (c) AjaMyAjax.com */ pbJSONLibReady = true; // notifies core that lib is available var pbJSONLibHeader = "[AjaMyAjax JSON Lib]"; this.processJSON = function() { var jsonData = (arguments[0]) ? arguments[0] : null; var strcontainer = (arguments[1]) ? arguments[1] : null; var strSearchElement = (arguments[2]) ? arguments[2] : null; var stroutputFormat = (arguments[3]) ? arguments[3] : null; if (jsonData) { // search for a specific element by passed parameter // note this is a native JavaScript JSON text parser... if (strSearchElement) { // remove quotes and bracketing tick marks only var srchkey = strSearchElement.replace(/(^'|'$|")/g, ""); if (!srchkey) { aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbJSONLibHeader, "Invalid search key parameter"); } return; } // if search string has any quotes, use "as is" for a case sensitive comparison // (example, pass a string parameter like this for an exact match: '"myString"') if (srchkey !== strSearchElement) { var srchtmp = jsonData; } else { // otherwise, will find any match var srchtmp = jsonData.toLowerCase(); var srchkey = strSearchElement.toLowerCase(); } var offset = srchtmp.indexOf(srchkey); if (offset == -1) { aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbJSONLibHeader, "Sorry, cannot retrieve data requested: " + strSearchElement); } return; } var jsonhash = ""; var jsontmp = jsonData; while (offset != -1) { var datastart = offset, dataend = offset; // find start/end of each name/value pair while (datastart >= 0) { if (jsontmp.charAt(datastart) == '{') { break; } else datastart--; } var jsontmplen = jsontmp.length; while (dataend < jsontmplen) { if (jsontmp.charAt(dataend) == '}') { break; } else dataend++; } if (dataend > datastart && dataend > 0) { jsonhash += "{" + jsontmp.substr(datastart + 1, dataend - datastart - 1) + "}"; } offset = srchtmp.indexOf(srchkey, dataend + 1); } // search results now a data subset to transform below jsonData = jsonhash; // clean up: jsonhash = null; jsontmp = null; srchkey = null; srchtmp = null; offset = null; datastart = null; dataend = null; } jsonData = json_addAnchorTags(jsonData); if (stroutputFormat == "HTABLE" || stroutputFormat == "VTABLE" || stroutputFormat == "ULIST") { // try to format json data into a clean table, // but there are many possible data formats.. var tablehdr = "<table id='myTableID'>"; if (!strSearchElement) { var lbracket = jsonData.indexOf("["); if (lbracket > 0) { tablehdr += "<th id='myHeaderID'>"; jsonData = jsonData.replace(/\[/, "</th><tr id='myRowID'>"); } else { jsonData = jsonData.replace(/\[/g, "<tr id='myRowID'>"); jsonData = jsonData.replace(/\]/g, "</tr>"); } if (jsonData.substr(0,1) == "{") { jsonData = tablehdr + jsonData.substr(1, jsonData.length); jsonData = jsonData.substr(0, jsonData.length-1) + "</table>"; } else { jsonData = tablehdr + jsonData + "</table>"; } } else { jsonData = tablehdr + "<tr id='myRowID'>" + jsonData + "</tr></table>"; } jsonData = jsonData.replace(/\[/g, "<tr id='myRowID'>"); jsonData = jsonData.replace(/\]/g, "</tr>"); jsonData = jsonData.replace(/\{/g, "<td>"); jsonData = jsonData.replace(/\}/g, "</td>"); jsonData = jsonData.replace(/[:,"]/g, ""); tablehdr = null; if (stroutputFormat == "VTABLE") { jsonData = jsonData.replace(/\<tr\>/g, ""); jsonData = jsonData.replace(/\<\/tr\>/g, ""); jsonData = jsonData.replace(/\<td\>/g, "<tr>"); jsonData = jsonData.replace(/\<\/td\>/g, "</tr>"); } if (stroutputFormat == "ULIST") { // then transform into a basic unordered list jsonData = jsonData.replace(/(\<table>|\<\/table>)/gi, ""); jsonData = jsonData.replace(/(\<th id='myHeaderID'>)/gi, "<th>"); jsonData = jsonData.replace(/(\<tr id='myRowID'>)/gi, "<tr>"); jsonData = jsonData.replace(/(th>|tr>|td>)/gi, "li>"); jsonData = jsonData.replace(/\<li\>\<\li\>/gi, "<li>"); jsonData = jsonData.replace(/\<\/li\>\<\/li\>/gi, "</li>"); jsonData = jsonData.replace(/\<li\>\<\/li\>/gi, ""); } } else { // transform json data into ordered text jsonData = jsonData.replace(/"\s+},/g, "<br/>"); jsonData = jsonData.replace(/[{:,"]/g, ""); jsonData = jsonData.replace(/[\[\]}]/g, "<br/>"); } // transform any tokens used jsonData = jsonData.replace(/hypertext-./gi, "http:"); aja_fill(strcontainer, jsonData); // clean up after: try { if (strcontainer) strcontainer = null; if (strSearchElement) strSearchElement = null; } finally { jsonData = null; } } } json_addAnchorTags = function(strmod) { if (strmod) { var srchtmp = strmod.toLowerCase(); if (srchtmp.indexOf("href") != -1) { // anchor markup already there return strmod; } // finds and adds multiple tags var linkstart = srchtmp.indexOf("http:"); while (linkstart != -1) { var linkend = linkstart; var jsontmp = strmod; // try to find the end of link var jsontmplen = jsontmp.length; while (linkend < jsontmplen) { if (/[ "}]/.test(jsontmp.charAt(linkend))) { break; } else linkend++; } if (linkend > linkstart && linkend > 0) { var stranchor = jsontmp.substr(linkstart, linkend - linkstart); jsontmp = jsontmp.replace(stranchor, "<a href='" + stranchor + "'>link</a>"); // use renamed key tokens to allow add'l anchor tag replaces jsontmp = jsontmp.replace(/http:/i, "hypertext-."); strmod = jsontmp; jsontmp = null; } else break; // start another search... srchtmp = strmod.toLowerCase(); linkstart = srchtmp.indexOf("http:"); } // clean up: srchtmp = null; linkstart = null; linkend = null; jsontmp = null; stranchor = null; } return strmod; }