在JS中,把 json 格式的字符串转成JSON对象,关键代码
json = eval('('+str+')');
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JSON应用</title> <script type="text/javascript"> function StrToJSON(str) { json = eval('('+str+')'); return json; } function ToJSON(data) { json = eval(data); return json; } // JSON数据 str = '{id:1,title:"你好,朋友",addtime:"2010-05-03"}'; strJson = StrToJSON(str); data = {id:2,title:"你好,哈哈",addtime:"2010-05-04"}; Json = ToJSON(data); document.write(strJson.id+"、 "+strJson.title+" "+strJson.addtime+"<br />"); document.write(Json.id+"、 "+Json.title+" "+Json.addtime+"<br />"); </script> </head> <body> </body> </html>