json字符串之间的相互转换示例代码

时间:2021-05-26

通过eval()函数可以将JSON字符串转化为对象,stringifier 函数的作用跟parse相反,用来将一个js对象转换为 JSON 文本

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>My JSP 'test5.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> <script type="text/javascript" src="<%=basePath%>/js/json2.js"></script> <script type="text/javascript"> alert("xxxx"); var t="{'firstName': 'cyra', 'lastName': 'richardson', 'address': {"+ "'streetAddress': '1 Microsoft way', 'city': 'Redmond', 'state': 'WA',"+ "'postalCode': 98052 },'phoneNumbers': [ '425-777-7777','206-777-7777' ] }"; var jsonobj=eval('('+t+')'); alert(t.firstName+" xxx"); alert(jsonobj.lastName); var t2="[{name:'zhangsan',age:'24'},{name:'lisi',age:'30'},{name:'wangwu',age:'16'},{name:'tianqi',age:'7'}] "; var myobj=eval(t2); for(var i=0;i<myobj.length;i++){ alert(myobj[i].name); alert(myobj[i].age); } var t3="[['<a href=# onclick=openLink(14113295100,社旗县国税局桥头税务所,14113295100,d6d223892dc94f5bb501d4408a68333d,swjg_dm);>14113295100</a>','社旗县国税局桥头税务所','社旗县城郊乡长江路西段']]"; //通过eval() 函数可以将JSON字符串转化为对象 var obj = eval(t3); for(var i=0;i<obj.length;i++){ for(var j=0;j<obj[i].length;j++){ alert(obj[i][j]); } var jsonText = JSON.stringify(obj); alert(jsonText); } </SCRIPT> </head> <body> This is my JSP page. <br> </body> </html>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章