JsonDecoder={
pos:0,
isDigit:function(ch){
return ( ch >= '0' && ch <= '9' )||( ch == "+" )||( ch == "-" )||( ch == "." );
},
isHexDigit:function( ch )
{
return ( JsonDecoder.isDigit( ch ) || ( ch >= 'A' && ch <= 'F' ) || ( ch >= 'a' && ch <= 'f' ) );
},
toTrue:function(str, from,to)
{
JsonDecoder.pos=from+3;
return true;
},
toFalse:function(str, from, to)
{
JsonDecoder.pos=from+4;
return false;
},
toNull:function(str, from, to)
{
JsonDecoder.pos=from+3;
return null;
},
toNAN:function (str, from, to)
{
JsonDecoder.pos=from+2;
return Number.NaN;
},
toNumber:function (str, from, to)
{
var i= from+1;
while(i<=to)
{
fl.trace(str);
var c = str.charAt(i);
if(!JsonDecoder.isDigit(c))
break
i++;
}
var num = Number(str.substring(from, i))
if(!isFinite(num))
throw{msg:"json格式错误,不正确的数字, 错误位置:"+from};
JsonDecoder.pos=i-1;
return num;
},
unescapeString:function( str )
{
var pos = 0;
var result="";
while(pos<str.length)
{
var c=str.charAt(pos);
if(c=="\\"){
if((pos+5<str.length) && str.charAt(pos+1)=="u")
{
var hexValue = "";
for ( var i= pos+2; i < pos + 6; i++ )
{
var c = str.charAt( i );
if ( !JsonDecoder.isHexDigit( c ) )
{
throw{msg:"json格式错误,字符串unicode编码转换错误,错误位置:"+i};
}
hexValue += c;
}
result += String.fromCharCode( parseInt( hexValue, 16 ) );
pos=pos+5;
}else{
result=result+c;
}
}else{
result=result+c;
}
pos=pos+1;
}
return result;
},
/*unescapeString:function( str )
{
return unescape(str);
},*/
toStr:function(str, from, to)
{
var ignor = false
for(var i = from+1; i<=to; i++)
{
var c=str.charAt(i);
if(!ignor)
{
if(c=="\"")
{
JsonDecoder.pos=i;
return JsonDecoder.unescapeString(str.substring(from+1, i));
//return str.substring(from+1, i);
}else if(c=="\\")
ignor=true;
}else{
ignor=false;
}
}

throw{msg:'json格式错误,字符串没有找到结尾, 错误位置:'+from};
},
toArray:function(str, from, to)
{
var arr = [];
JsonDecoder.pos=from+1;
while(JsonDecoder.pos<=to)
{
var c = str.charAt(JsonDecoder.pos);
switch(c)
{
case "\"":
arr.push(JsonDecoder.toStr(str, JsonDecoder.pos, to));
break;
case "[":
arr.push(JsonDecoder.toArray(str, JsonDecoder.pos, to));
break;
case "{":
arr.push(JsonDecoder.toObject(str, JsonDecoder.pos, to));
break;
case "]":
return arr;
case "f":
arr.push(JsonDecoder.toFalse(str, JsonDecoder.pos, to));
break;
case "F":
arr.push(JsonDecoder.toFalse(str, JsonDecoder.pos, to));
break;
case "t":
arr.push(JsonDecoder.toTrue(str, JsonDecoder.pos, to));
break;
case "T":
arr.push(JsonDecoder.toTrue(str, JsonDecoder.pos, to));
break;
case "n":
arr.push(JsonDecoder.toNull(str, JsonDecoder.pos, to));
break;
case "N":
arr.push(JsonDecoder.toNAN(str, JsonDecoder.pos, to));
break;
default:
if(JsonDecoder.isDigit(c))
arr.push(JsonDecoder.toNumber(str, JsonDecoder.pos, to));
}
JsonDecoder.pos=JsonDecoder.pos+1;
}
throw{msg: 'json格式错误,表没有找到结尾, 错误位置:'+from};
},
toObject:function(str, from, to)
{
var obj = {};
var key;
JsonDecoder.pos=from+1;
while(JsonDecoder.pos<=to)
{
var c = str.charAt(JsonDecoder.pos);
switch(c)
{
case "\"":
if(!key)
{
key=JsonDecoder.toStr(str, JsonDecoder.pos, to);
}else{
obj[key] = JsonDecoder.toStr(str,JsonDecoder.pos,to);
key = null;
}
break;
case "[":
if(!key){
key=JsonDecoder.toArray(str, JsonDecoder.pos, to);
}else{
obj[key]=JsonDecoder.toArray(str, JsonDecoder.pos, to);
key=null;
}
break;
case "{":
if(!key){
key=JsonDecoder.toObject(str, JsonDecoder.pos, to);
}else{
obj[key]=JsonDecoder.toObject(str, JsonDecoder.pos, to);
key=null;
}
break;
case "}":
return obj;
case "]":
return obj;
case "f":
if(!key)
throw{msg:"json格式错误,boolean不能做key, 错误位置:"+from};
obj[key]=JsonDecoder.toFalse(str, JsonDecoder.pos, to);
break;
case "F":
if(!key)
throw{msg:"json格式错误,boolean不能做key, 错误位置:"+from};
obj[key]=JsonDecoder.toFalse(str, JsonDecoder.pos, to);
break;
case "t":
if(!key)
throw{msg:"json格式错误,boolean不能做key, 错误位置:"+from};
obj[key]=JsonDecoder.toTrue(str, JsonDecoder.pos, to);
case "T":
if(!key)
throw{msg:"json格式错误,boolean不能做key, 错误位置:"+from};
obj[key]=JsonDecoder.toTrue(str, JsonDecoder.pos, to);
break;
case "n":
if(!key)
throw{msg:"json格式错误,null不能做key, 错误位置:"+from};
obj[key]=JsonDecoder.toNull(str, JsonDecoder.pos, to);
break;
case "N":
if(!key)
throw{msg:"json格式错误,nan不能做key, 错误位置:"+from};
obj[key]=JsonDecoder.toNAN(str, JsonDecoder.pos, to);
break;
default:
if(JsonDecoder.isDigit(c)){
if(!key){
key=JsonDecoder.toNumber(str, JsonDecoder.pos, to);
}else{
obj[key]=JsonDecoder.toNumber(str, JsonDecoder.pos, to);
key=null;
}
}
}
JsonDecoder.pos = JsonDecoder.pos + 1
}
throw{msg:'json格式错误,表没有找到结尾, 错误位置:}'+from};
},
decode:function(str)
{
JsonDecoder.pos=0;
if(str=="")
return null;
var c=str.charAt(JsonDecoder.pos);
switch(c)
{
case "\"":
return JsonDecoder.toStr(str, JsonDecoder.pos, str.length-1);
case "[":
return JsonDecoder.toArray(str, JsonDecoder.pos, str.length-1);
case "{":
return JsonDecoder.toObject(str, JsonDecoder.pos, str.length-1);
case "f":
return false;
case "F":
return false;
case "t":
return true;
case "n":
return null;
case "N":
return Number.NaN;
case "T":
return true;
default:
if(JsonDecoder.isDigit(c))
return JsonDecoder.toNumber(str, JsonDecoder.pos, str.length-1);
}
},
}

JsonEncoder={
fromNumber:function(n){
return String(n);
},
fromBoolean:function(b)
{
return b?"true":"false";
},
escapeString:function(str){
return escape(str);
},
fromStr:function(s){
return "\""+JsonEncoder.escapeString(s)+"\"";
},
fromUndefined:function(){
return "null";
},
fromObject:function(obj){
var s="{";
for(var k in obj)
{
s=s+JsonEncoder.encode(k)+":"+JsonEncoder.encode(obj[k]);
s=s+",";
}
return s.slice(0, -1)+"}";
},
fromArray:function(arr){
var s="[";
for(var ii=0; ii<arr.length; ii++)
{
s=s+JsonEncoder.encode(arr[ii]);
if(ii!=arr.length-1){
s=s+",";
}
}
return s+"]";
},
fromFunction:function(){
error("can not encode function type");
},
encode:function(obj)
{
if(obj instanceof Array)
return JsonEncoder.fromArray(obj);
switch(typeof(obj))
{
case "number":
return JsonEncoder.fromNumber(obj);
case "boolean":
return JsonEncoder.fromBoolean(obj);
case "string":
return JsonEncoder.fromStr(obj);
case "function":
return JsonEncoder.fromFunction(obj);
case "object":
return JsonEncoder.fromObject(obj);
case "undefined":
return JsonEncoder.fromUndefined(obj);
defualt:
error("error kind");
}
},
}

Json={
encode:JsonEncoder.encode,
decode:JsonDecoder.decode,
}

Object.prototype.toString=function()
{
var kind=typeof(this);
switch(typeof(obj))
{
case "number":
return String(this)
case "boolean":
return JsonEncoder.fromBoolean(obj);
case "string":
return JsonEncoder.fromStr(obj);
case "function":
return JsonEncoder.fromFunction(obj);
case "object":
return JsonEncoder.fromObject(obj);
case "undefined":
return JsonEncoder.fromUndefined(obj);
defualt:
error("error kind");
}
}

Array.prototype.toString=function()
{
var s="[";
var bempty=true;
for(var ii=0; ii<this.length; ii++)
{
bempty=false;
s=s+String(this[ii])+",";
}
if(!bempty){
s=s.slice(0, -1);
}
return s+"]";
}

Object.prototype.toString=function()
{
var s="{";
var bempty=true;
for(var k in this)
{
bempty=false;
s=s+String(k)+":"+String(this[k])+",";
}
if(!bempty){
s=s.slice(0, -1);
}
return s+"}";
}

Js版json解析的更多相关文章

  1. COCOS2D - JS 之JSON 解析

    list 类型的json数据  var source = ["10004","1234","4","3","1 ...

  2. js对json解析获取对应属性的值,JSON.stringify()和JSON.parse()

    JSON.stringify() 该方法,将一个JSON对象转化为字符串string JSON.parse() 该方法,将一个字符串转化为JSON对象object 对于JSON对象,获取其对应键值 可 ...

  3. 一个用C++写的Json解析与处理库

    什么是Json?这个库能做什么? JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is e ...

  4. 《项目经验》--后台一般处理程序向前台JS文件传递JSON,JS解析JSON,将数据显示在界面--显示在DropDownList 或 显示在动态创建的table中

    http://blog.csdn.net/mazhaojuan/article/details/8599167 先看一下我要实现的功能界面: 这篇文章主要介绍:后台一般处理程序把从数据库查找的数据,转 ...

  5. JS中将JSON的字符串解析成JSON数据格式《转》

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 使用eval函数来解析,并且使用jquery的ea ...

  6. js中eval详解,用Js的eval解析JSON中的注意点

    先来说eval的用法,内容比较简单,熟悉的可以跳过eval函数接收一个参数s,如果s不是字符串,则直接返回s.否则执行s语句.如果s语句执行结果是一个值,则返回此值,否则返回undefined. 需要 ...

  7. 用Js的eval解析JSON中的注意点

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 使用eval函数来解析,并且使用jquery的ea ...

  8. PureMVC(JS版)源码解析:总结

    PureMVC源码中设计到的11个类已经全部解析完了,回首想想,花了一周的时间做的这点事情还是挺值得的,自己的文字组织表达能力和对pureMVC的理解也在写博客的过程中得到了些提升.我也是第一次写系列 ...

  9. PureMVC(JS版)源码解析

    PureMVC(JS版)源码解析:总结   PureMVC源码中设计到的11个类已经全部解析完了,回首想想,花了一周的时间做的这点事情还是挺值得的,自己的文字组织表达能力和对pureMVC的理解也在写 ...

随机推荐

  1. 【转】Linux 中清空或删除大文件内容的五种方法(truncate 命令清空文件)

    原文: http://www.jb51.net/article/100462.htm truncate -s 0 access.log -------------------------------- ...

  2. Bounded Context

    From http://martinfowler.com/bliki/BoundedContext.html Bounded Context is a central pattern in Domai ...

  3. react-native 初始化 各种报错 及 解决方案

    1.Unable to load script from assets 'index.android.bundle'. curl -k "http://localhost:8081/inde ...

  4. 全球IT管理最佳实践之DevOps Master 认证

    原文:http://soft.chinabyte.com/30/13940030.shtml 作者:国际最佳实践管理联盟 孙振鹏 关键字: DevOps.DevOps认证.DevOpsDays.Dev ...

  5. AIX 安装标准

    文件夹 一.网卡需求 二.光纤卡需求 三.磁盘需求 四.主机文件系统需求 五.主机名命名规范 六.安装设置规范 七.參数改动规范 八.时钟同步设置 九.rootvg做镜像 十.AIX系统安全加固 一. ...

  6. Atitit.jdk java8的语法特性详解 attilax 总结

    Atitit.jdk java8的语法特性详解 attilax 总结 1.1. 类型推断这个特别有趣的.鲜为人知的特性1 2. Lambda1 2.1. 内部迭代意味着改由Java类库来进行迭代,而不 ...

  7. centos7 firefox 安装flash

    在官网下载flash的tar包 https://get.adobe.com/flashplayer/?spm=a2h0j.8191423.movie_player.5~5~5~8~A 在下载tar包的 ...

  8. python 的三元表达式

    python中的三目运算符不像其他语言 其他的一般都是 判定条件?为真时的结果:为假时的结果 如 result=5>3?1:0 这个输出1,但没有什么意义,仅仅是一个例子. 而在python中的 ...

  9. 笔试真题解析 ALBB-2015 系统project师研发笔试题

    4)在小端序的机器中,假设 union X {     int x;     char y[4]; }; 假设 X a; a.x=0x11223344;//16进制 则:() y[0]=11 y[1] ...

  10. jfreechart折线图 demo

    public class ChartUtil { public static ChartUtil chartUtil; private RoomViewsDataService roomViewsDa ...