eval json 的2种方式

 

 
  

 

 

//单个情况

str1 = "{id:"+ id+",name:"+ name+"}"; eval("(" + str1+ ")");

//多个情况

str2  = "{[{...},{...}]}"   eval(str2);

 location.search取每一个参数的值 转换json格式
//?pname=14 - 解脫&author='aaa'&copyright='bbb'&info='cccc''

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
var arrurlvalue = "" ;
var urlvalue ;
var url = location.search;
url
= url.substring( 1 ,url.length);
var arrurl = url.split( ' & ' );
arrurlvalue
= " { " ;
for ( var i = 0 ;i < arrurl.length;i ++ ){
if (i == arrurl.length - 1 )
arrurlvalue
+= arrurl[i].split( ' = ' )[ 0 ] + " : " + " ' " + arrurl[i].split( ' = ' )[ 1 ] + " ' " ;
else
arrurlvalue
+= arrurl[i].split( ' = ' )[ 0 ] + " : " + " ' " + arrurl[i].split( ' = ' )[ 1 ] + " ' " + " , " ;

}
//{pname:'14 - 解脫',pauthor:'aaa',pcopyright:'bbb',pinfo:'cccc'}
arrurlvalue += " } " ;
var p = eval("(" + arrurlvalue+ ")" );
alert(p.pname);

 

[
 [
   {"id":0,"width":85.608,"top":0,"left":0,"oy":0,"name":"左侧窗口","ox":0,"height":240,"ms":0,"ex":0,"zindex":1200,"cursor":"","mt":0},
   {"id":1,"width":96,"top":0,"left":144,"oy":0,"name":"右上窗口","ox":0,"height":120,"ms":0,"ex":0,"zindex":1201,"cursor":"","mt":0},
   {"id":2,"width":96,"top":120,"left":144,"oy":0,"name":"右下窗口","ox":0,"height":120,"ms":0,"ex":0,"zindex":1202,"cursor":"","mt":0}
 ],
 [
   {"id":0,"width":240,"top":0,"left":0,"oy":0,"name":"上窗口","ox":0,"height":230.4,"ms":0,"ex":0,"zindex":1200,"cursor":"","mt":0},
   {"id":1,"width":240,"top":230.4,"left":0,"oy":0,"name":"字幕窗口","ox":0,"height":9.6,"ms":0,"ex":0,"zindex":1201,"cursor":"","mt":0}
 ],
 [
   {"id":0,"width":240,"top":0,"left":0,"oy":0,"name":"满屏","ox":0,"height":240,"ms":0,"ex":0,"zindex":1200,"cursor":"","mt":0}
 ]
]

{[
{"i":1,"id":355,"bg":"","bgi":"","isdel":"0","tv":5,"d":3600,"p":0,"s":"","t":"音视","ds":"01:00:00","n":"Test File #9"}, 
{"i":1,"id":354,"bg":"","bgi":"","isdel":"0","tv":3,"d":3600,"p":100,"s":"","t":"图片","ds":"01:00:00","n":"q_2"}, 
{"i":2,"id":356,"bg":"","bgi":"","isdel":"0","tv":-3,"d":4027,"p":0,"s":"888","t":"直播","ds":"01:07:07","n":"888"}
]}


[{
  persons:
  [{name:"jordan",sex:"m",age:"40"},
   {name:"bryant",sex:"m",age:"28"},
   {name:"McGrady",sex:"m",age:"27"}
  ]
}]
有[    UserList[0].pname

var UserList = [
{pname:'14 - 解脫',pauthor:'aaa',pcopyright:'bbb',pinfo:'cccc',pduration:272,
    chapter:
    [
      {pc_order:0,pc_duration:240,pc_program_id:533},
      {pc_order:0,pc_duration:272,pc_program_id:533}
    ]
}
];
没有[   UserList.pname   chapter: [{},{}]必须要[

var UserList =
{pname:'14 - 解脫',pauthor:'aaa',pcopyright:'bbb',pinfo:'cccc',pduration:272,
    chapter:
    [
      {pc_order:0,pc_duration:240,pc_program_id:533},
      {pc_order:0,pc_duration:272,pc_program_id:533}
    ]
};

转载于:https://www.cnblogs.com/reommmm/archive/2010/02/26/1674045.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值