JSON解析器—实现方案
javascript对象表示法(javascript Object Notation,简称JSON)是一个轻量级的数据交换格式,他是基于js的对象字面量表示法。
经过长期的学习和使用,参考相关书籍,编写了一个JSON解析器:
即将它封装成了一个插件 文件名:json_parse.js,如下:
var json_parse =function () { //这是一个能json文本解析成js数据结构的函数 //递归降序的解析器 //我们在零一个函数中定义此函数,以避免创建全局变量 var curIndex,//当前字符的索引 curChar,//当前字符 escapee = { '"' : '"', '\\' : '\\', '/' : '/', b : 'b', f : '\f', n : '\n', r : '\r', t : '\t' }, text, error = function (msg) { //当某处错误时,调用error throw { name :"SyntaxError", message: msg, curIndex: curIndex , text :text }; }, next = function (cur) { //如果提供了参数c,那么检验他是否匹配当前字符 if (cur && cur !==curChar) { error("Excected'" + c + "' instead of '" + curChar + "'"); } //获取下一个字符,当没有下一个字符时,返回一个空字符串 curChar =text.charAt(curIndex); curIndex += 1; return curChar; }, number = function () { //解析一个数字值 var number, string = ''; if (curChar === '-') { string = '-'; next('-'); } while (curChar >= '0'&& curChar <= '9') { string += curChar; next(); } if (curChar === '.') { string += '.'; while (next()&& curChar >= '0' && curChar <= '9') { string +=curChar; } } if (curChar === 'e' ||curChar ==='E') { string += curChar; next(); if (curChar === '-'|| curChar === '+') { string +=curChar; next(); } while (curChar >='0' && curChar <= '9') { string +=curChar; next(); } } number = + string; if (isNaN(number)) { error("Badnumber"); } else { return number; } }, string = function () { //解析一个字符串值 var hex, i, string = '',uffff; //当解析字符串值时,我们必须要找到"和\字符 if (curChar === '"') { while (next()) { if (curChar=== '"') { next(); returnstring; } else if(curChar ==='\\') { next(); if(curChar === 'u') { uffff= 0; for(i = 0; i < 4; i += 1 ) { hex= parseInt(next(), 16); if(!isFinite(hex)) { break; } uffff= uffff*16 + hex; } string+= String.formatCharCode(uffff); } elseif (typeof escapee[curChar] === 'string') { string+= escapee[curChar]; } else{ break; } } else { string+= curChar; } } } error("Badstring"); }, //跳过空格 white = function () { while (curChar&& curChar <= ' ') { next(); } }, word = function () { //true,false,null switch (curChar) { case 't': next('t'); next('r'); next('u'); next('e'); case 'f': next('f'); next('a'); next('l'); next('s'); next('e'); case 'n': next('n'); next('u'); next('l'); next('l'); } error("Unexpected'" + curChar + "'"); }, value,//值函数的占位符 array = function () { //解析一个数值值 var array = []; if (curChar === '['){ next('['); white(); if (curChar === ']'){ next(']'); returnarray;//空数组 } while (curChar) { array.push(value()); white(); if (curChar=== ']') { next(']'); returnarray; } next(','); white(); } } error("Badarray"); }, object = function () { var key, object = {}; if (curChar === '{') { next('{'); white(); if (curChar ==='}'){ next('}'); returnobject; //空对象 } while (curChar) { key =string(); white(); next(':'); object[key] =value(); white(); if (curChar=== '}') { next('}'); returnobject; } next(','); white(); } } error("Badobject"); }; value = function () { //解析一个json值,他可以是对象,数组,字符串,数字,或者一个词 white(); switch (curChar) { case '{': returnobject(); case '[': returnarray(); case '"': returnstring(); case '-': returnnumber(); default: return curChar >='0' && curChar <= '9' ? number() : word(); } }; //返回json_parse 函数 return function (source, reviver){ var result; text = source; curIndex = 0; curChar = ' '; result = value(); white(); if (curChar) { error("Synaxerror"); } //如果存在reviver函数,我们就递归对这个新结构调用walk函数, //开始时,先创建一个临时的启动对象,并以一个空字符串作为键名保存结果, //然后传递每个”名/值“对给reviver函数去处理可能存在的转换; //如果没有reviver函数,我们就简单的返回这个结果 return typeof reviver ==='function' ? function walk(holder, key){ var k, v, value =holder[key]; if (value &&typeof value === 'object') { for (k invalue) { if(Object.hasOwnProperty.call(value, k)) { v= walk(value, k) if(v != undefined) { value[k]= v; }else { deletevalue[k]; } } } } returnreviver.call(holder, key, value); }({'': result}, ''): result; }; }();
测试了一下:
Object {name: "jxj", age: 24, score: Object, hobb: Array[2]}
age: 24
hobb: Array[2]
name: "jxj"
score: Object
__proto__: Object
功能基本正常,呵呵呵
转载于:https://blog.51cto.com/shuizhongyue/1601673